DateAdd problem with month end
Posted
#1
(In Topic #840)
Regular

DateAdd doesn't do this for me.
If Date1 is 30 April 2022 then
Code
DateAdd(Date1, gb.Month, 1)Is there an elegant way to achieve this?
Gambas 3.17.2
Posted
Guru

Code (gambas)
- iYear += 1
- iMonth = 1
- iMonth = iLoop
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
Posted
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
^
Posted
Guru

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.
Posted
Enthusiast

Posted
Guru

Posted
Enthusiast

Output :
today Is 04/17/2022
Yesterday was 04/17/2022
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
Posted
Enthusiast

results :
today Is 04/17/2022
Yesterday was 04/16/2022
Tomorrow it will be 04/18/2022 05:00:00
Posted
Guru

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

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

Code (gambas)
- iYear += 1
- iMonth = 1
- iMonth = iLoop
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
Posted
Regular

Yesterday = Date(now) - 1
Look at how gambas represents dates internally
1 guest and 0 members have just viewed this.


