Page 1 of 2

DateAdd problem with month end

Posted: Sunday 17th April 2022 7:52am
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

Re: DateAdd problem with month end

Posted: Sunday 17th April 2022 11:27am
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

Re: DateAdd problem with month end

Posted: Sunday 17th April 2022 3:47pm
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
^

Re: DateAdd problem with month end

Posted: Sunday 17th April 2022 4:54pm
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.

Re: DateAdd problem with month end

Posted: Sunday 17th April 2022 5:07pm
by grayghost4
for me Print day(1) .... returns 0

Re: DateAdd problem with month end

Posted: Sunday 17th April 2022 6:27pm
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.

Re: DateAdd problem with month end

Posted: Sunday 17th April 2022 9:19pm
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 ;)

Re: DateAdd problem with month end

Posted: Sunday 17th April 2022 10:25pm
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

Re: DateAdd problem with month end

Posted: Monday 18th April 2022 11:08am
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

Re: DateAdd problem with month end

Posted: Monday 18th April 2022 11:41am
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 .