DateAdd problem with month end

Post

Posted
Rating:
#1 (In Topic #840)
Regular
bill-lancaster is in the usergroup ‘Regular’
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

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

Is there an elegant way to achieve this?

Gambas 3.17.2
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
Not sure that this is 'elegant', but it works: -

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim iLoop, iMonth As Integer
  4.   Dim iYear As Integer = 2022
  5.   Dim dDate As Date
  6.  
  7.   For iLoop = 2 To 13
  8.     If iLoop = 13 Then
  9.       iYear += 1
  10.       iMonth = 1
  11.     Else
  12.       iMonth = iLoop
  13.     End If
  14.     dDate = Date(iYear, iMonth, 1) - Day(1)
  15.     Print Format(dDate, "dd/mm/yyyy") & "   --   " & Format(dDate, "dddd d mmmm yyyy")
  16.   Next
  17.  

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
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
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
^
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
for me Print day(1) .... returns 0
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Guru
cogier is in the usergroup ‘Guru’
What do you get running the following?: -

Code (gambas)

  1.   Dim dDate As Date
  2.  
  3.   dDate = Date(Now) - Day(1)
  4.   Print "Yesterday was " & Format(dDate, "mm/dd/yyyy")
  5.  

Note that the code in my other posts were using Day/Month/Year format not, as in this example, the USA date format.
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’

Code (gambas)

  1. Dim dDate As Date
  2.  
  3. dDate = Date(Now) - Day(1)
  4.  
  5. Print "today Is  " & Format(Date(Now), "mm/dd/yyyy")
  6.  
  7. Print "Yesterday was " & Format(dDate, "mm/dd/yyyy")
  8.  

 Output :

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

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim dDate As Date
  4.  
  5. dDate = Date(Now) - Day(2)
  6.  
  7. Print "today Is  " & Format(Date(Now), "mm/dd/yyyy")
  8.  
  9. Print "Yesterday was " & Format(dDate, "mm/dd/yyyy")
  10.  

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   ;)
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
this does work correctly

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim dDate As Date
  4.  
  5. dDate = Date(Now - 1)
  6.  
  7. Print "today Is      " & Format(Date(Now), "mm/dd/yyyy")
  8.  
  9. Print "Yesterday was " & Format(dDate, "mm/dd/yyyy")
  10.  
  11. Print "Tomorrow it will be   " & Date(Now + 1)
  12. End  

results :
today Is      04/17/2022
Yesterday was 04/16/2022
Tomorrow it will be   04/18/2022 05:00:00
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Guru
cogier is in the usergroup ‘Guru’
Well there is something wrong somewhere as your examples work as expected at this end, QT, GTK returned the same results.

<IMG src="https://www.cogier.com/gambas/DateAdd1.png"> </IMG>

<IMG src="https://www.cogier.com/gambas/DateAdd2.png"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
 Ubuntu 21.10 … Gnome …. X11

tried with 3.16 and now 3.17
I may try Wayland and see if that makes a difference .
Online now: No Back to the top

Post

Posted
Rating:
#11
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim iLoop, iMonth As Integer
  4.   Dim iYear As Integer = 2022
  5.   Dim dDate As Date
  6.  
  7.   For iLoop = 2 To 13
  8.     If iLoop = 13 Then
  9.       iYear += 1
  10.       iMonth = 1
  11.     Else
  12.       iMonth = iLoop
  13.     End If
  14.     dDate = Date(iYear, iMonth, 1) - 1   ' '  <<<<  this does work
  15.     Print Format(dDate, "dd/mm/yyyy") & "   --   " & Format(dDate, "dddd d mmmm yyyy")
  16.   Next
  17.  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
Online now: No Back to the top

Post

Posted
Rating:
#12
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 Forget the Day(1) just use, for example
Yesterday =  Date(now)  - 1
Look at how gambas represents dates internally

Online now: No Back to the top
1 guest and 0 members have just viewed this.