DateAdd problem with month end

Post your Gambas programming questions here.
bill-lancaster
Posts: 190
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

DateAdd problem with month end

Post by bill-lancaster »

I want a series of dates where each date is a month end.
DateAdd doesn't do this for me.
If Date1 is 30 April 2022 then

Code: Select all

 DateAdd(Date1, gb.Month, 1)
gives 30 May 2022 not 31 May

Is there an elegant way to achieve this?

Gambas 3.17.2
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: DateAdd problem with month end

Post by cogier »

Not sure that this is 'elegant', but it works: -
Public Sub Form_Open()

  Dim iLoop, iMonth As Integer
  Dim iYear As Integer = 2022
  Dim dDate As Date

  For iLoop = 2 To 13
    If iLoop = 13 Then
      iYear += 1
      iMonth = 1
    Else
      iMonth = iLoop
    End If
    dDate = Date(iYear, iMonth, 1) - Day(1)
    Print Format(dDate, "dd/mm/yyyy") & "   --   " & Format(dDate, "dddd d mmmm yyyy")
  Next

End
Result: -
31/01/2022 -- Monday 31 January 2022
28/02/2022 -- Monday 28 February 2022
31/03/2022 -- Thursday 31 March 2022
30/04/2022 -- Saturday 30 April 2022
31/05/2022 -- Tuesday 31 May 2022
30/06/2022 -- Thursday 30 June 2022
31/07/2022 -- Sunday 31 July 2022
31/08/2022 -- Wednesday 31 August 2022
30/09/2022 -- Friday 30 September 2022
31/10/2022 -- Monday 31 October 2022
30/11/2022 -- Wednesday 30 November 2022
31/12/2022 -- Saturday 31 December 2022
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: DateAdd problem with month end

Post by grayghost4 »

dDate = Date(iYear, iMonth, 1) - Day(1)
what is the purpose of "-Day(1) ?

seems to work the same without it.


dDate = Date(iYear, iMonth, 1) - Day(2)

outputs :

31/01/2022 -- Monday 31 January 2022
28/02/2022 -- Monday 28 February 2022
31/03/2022 -- Thursday 31 March 2022
30/04/2022 -- Saturday 30 April 2022
31/05/2022 -- Tuesday 31 May 2022
30/06/2022 -- Thursday 30 June 2022
31/07/2022 -- Sunday 31 July 2022
31/08/2022 -- Wednesday 31 August 2022
30/09/2022 -- Friday 30 September 2022
31/10/2022 -- Monday 31 October 2022
30/11/2022 -- Wednesday 30 November 2022
31/12/2022 -- Saturday 31 December 2022
^
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: DateAdd problem with month end

Post by cogier »

Hi greyghost4,

The idea was to get the beginning of the following month and go back 1 day, hence - Day(1)

I tried your code: - dDate = Date(iYear, iMonth, 1) - Day(2)
Result: -
30/01/2022 -- Sunday 30 January 2022
27/02/2022 -- Sunday 27 February 2022
30/03/2022 -- Wednesday 30 March 2022
29/04/2022 -- Friday 29 April 2022
30/05/2022 -- Monday 30 May 2022
29/06/2022 -- Wednesday 29 June 2022
30/07/2022 -- Saturday 30 July 2022
30/08/2022 -- Tuesday 30 August 2022
29/09/2022 -- Thursday 29 September 2022
30/10/2022 -- Sunday 30 October 2022
29/11/2022 -- Tuesday 29 November 2022
30/12/2022 -- Friday 30 December 2022

I tried without the -Day(1): - dDate = Date(iYear, iMonth, 1)
Result: -
01/02/2022 -- Tuesday 1 February 2022
01/03/2022 -- Tuesday 1 March 2022
01/04/2022 -- Friday 1 April 2022
01/05/2022 -- Sunday 1 May 2022
01/06/2022 -- Wednesday 1 June 2022
01/07/2022 -- Friday 1 July 2022
01/08/2022 -- Monday 1 August 2022
01/09/2022 -- Thursday 1 September 2022
01/10/2022 -- Saturday 1 October 2022
01/11/2022 -- Tuesday 1 November 2022
01/12/2022 -- Thursday 1 December 2022
01/01/2023 -- Sunday 1 January 2023

Sorry, but I can't explain your results, as you can see all my experimentation returned what I expected.
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: DateAdd problem with month end

Post by grayghost4 »

for me Print day(1) .... returns 0
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: DateAdd problem with month end

Post by cogier »

What do you get running the following?: -
  Dim dDate As Date

  dDate = Date(Now) - Day(1)
  Print "Yesterday was " & Format(dDate, "mm/dd/yyyy")
Note that the code in my other posts were using Day/Month/Year format not, as in this example, the USA date format.
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: DateAdd problem with month end

Post by grayghost4 »

Dim dDate As Date
 
dDate = Date(Now) - Day(1)

Print "today Is  " & Format(Date(Now), "mm/dd/yyyy")

Print "Yesterday was " & Format(dDate, "mm/dd/yyyy")
Output :

today Is 04/17/2022
Yesterday was 04/17/2022
Public Sub Form_Open()
 
  Dim dDate As Date
 
dDate = Date(Now) - Day(2)

Print "today Is  " & Format(Date(Now), "mm/dd/yyyy")

Print "Yesterday was " & Format(dDate, "mm/dd/yyyy")
output :

today Is 04/17/2022
Yesterday was 04/16/2022

It has nothing to do with the format .... I tried it with GTK3 and QT4 and 5 same results
I upgraded to 3.17 ... same results ;)
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: DateAdd problem with month end

Post by grayghost4 »

this does work correctly
Public Sub Form_Open()
 
  Dim dDate As Date
 
dDate = Date(Now - 1)

Print "today Is      " & Format(Date(Now), "mm/dd/yyyy")

Print "Yesterday was " & Format(dDate, "mm/dd/yyyy")

Print "Tomorrow it will be   " & Date(Now + 1)
End  
results :
today Is 04/17/2022
Yesterday was 04/16/2022
Tomorrow it will be 04/18/2022 05:00:00
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: DateAdd problem with month end

Post by cogier »

Well there is something wrong somewhere as your examples work as expected at this end, QT, GTK returned the same results.

Image

Image
User avatar
grayghost4
Posts: 174
Joined: Wednesday 5th December 2018 5:00am
Location: Marengo, Illinois usa

Re: DateAdd problem with month end

Post by grayghost4 »

Ubuntu 21.10 ... Gnome .... X11

tried with 3.16 and now 3.17
I may try Wayland and see if that makes a difference .
Post Reply