Help with adding a month (related to time in general, not only the code)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

Help with adding a month (related to time in general, not only the code)

08 Dec 2018, 10:54

Hello, i didn't sleep so here goes a post that might be retarded or a good question.

If i have January, 29 of 1999 (no leap year)
And i add 1 month, should i get February, 28 of 1999 or March, 1 of 1999?

Why this question?
Because if i add 2 months to January, 29 of 1999 (no leap year) i get March, 29 of 1999
but if i add 1 month to January, 29 of 1999 (no leap year) and i get February, 28 of 1999 and then i add 1 more month i get March, 28 of 1999

My head is hurting right now, idk if i'm being stupid or if i have a good question.

This is not only for ahk, i mean this is for real life, what if i say "i will do it in 1 month" and i'm in January, 29 of a non-leap year? will i do it in February, 28 or March, 1?

I used a lot of calculators on the web and the results i got were:
Jan, 29 + 1 months= Feb 28
Feb 28 +1 months = March 28
BUT
Jan, 29 + 2 = March 29

What does "adding x month/s" mean? Does it mean, "add the number of days in the current month so if you are in january you add 31" ? because in this Jan, 29 + 1 months on a non-leap year should return March 1 (january + 3 days = february 1 + 28 = march 1).

Thanks in advance.
kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

Re: Help with adding a month (related to time in general, not only the code)

08 Dec 2018, 11:18

To add more to this:
if i do 0+1 and then +10 it is = 11. It is the same as doing 0+11=11 or 0 + (1+10) = 11

This means that by adding the same i get the same result, so if i do 0+1+1= 0+2

So why if i add 1 month and then 1 month it is not equal to adding 2 months, being month a "variable" that contains the amount of days of the current month.

If i use the examples i gave on the first post but by days ON A NON LEAP YEAR:
(day of the year, for example 29 of 365 days means January 29)
January 29 = 29
February 28 = 59
March 1 = 60
So if months have this amount of days: 31,28,31,30,31,30,31,31,30,31,30,31
Then 29 + 31 (adding a month, 31 days because we are in january) = 60 (march 1)
or if i do what the calculator do: 29 + 30 (adding amonth, 30 days because fuck 31 days january) = 59 (febryary 28)
If i say "12 months" IN A NON LEAP YEAR i'm saying "365 days" or "31+28+31+30+31+30+31+31+30+31+30+31=365 = 12 months"
So i can't say that a month in January IN A NON LEAP YEAR equals 30 because if i do that then the math would change to: "30+28+31+30+31+30+31+31+30+31+30+31=365 = 12 months" = 364 days, missing 1 day
kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

Re: Help with adding a month (related to time in general, not only the code)

08 Dec 2018, 11:50

To add even more:

If i do 28, 29, 30 or 31 of january + 1 month i get february 28 on all of those cases. So a month equals 31 in first case, 30 on second, 29 on third, and 28 on fourth.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Help with adding a month (related to time in general, not only the code)

09 Dec 2018, 10:29

- I wanted a function to add/subtract n months to/from a date.
- I chose the following approach:
Jan 29 + 1 month -> Feb 29 (invalid) -> Feb 28
Jan 29 + 2 months -> Mar 29
Jan 29 + 3 months -> Apr 29
Jan 31 + 1 month -> Feb 31 (invalid) -> Feb 30 (invalid) -> Feb 29 (invalid for a 365-day year) -> Feb 28
Jan 31 + 2 months -> Mar 31
Jan 31 + 3 months -> Apr 31 (invalid) -> Apr 30
- So the function increases the month by n, and if the date is invalid, decreases the day by 1 until a valid date is reached.
- For me this appeared to be the best approach. Perhaps there are arguments for a slightly different approach, e.g. if a day is invalid, use the first day of the next month. There are pros and cons of whatever approach.
- Depending on the situation you might choose an arbitrary definition such as 31 days, or 30 days, or n working days. Maybe define a month as 365/12 = 30.416667 days, and add n of those.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
kyuuuri
Posts: 340
Joined: 09 Jan 2016, 19:20

Re: Help with adding a month (related to time in general, not only the code)

09 Dec 2018, 18:08

I made a function that adds days until the next month is reached, then adds the rest of the days, this way i don't have invalid dates.
The problem is, i think the correct way is:
January 29 + 1 month = Mar 1 because this way i don't break the 365 days year.

But that's what i think, what i wanted to know is that if there is a "correct way" to do this. I mean, in maths 1+1 = 2, and that's the only answer. I wanted to know if there is something like this for this case.

Why i don't use thist approach:
jeeswg wrote:
09 Dec 2018, 10:29
- I wanted a function to add/subtract n months to/from a date.
- I chose the following approach:
Jan 29 + 1 month -> Feb 29 (invalid) -> Feb 28
Jan 29 + 2 months -> Mar 29
Jan 29 + 3 months -> Apr 29
Jan 31 + 1 month -> Feb 31 (invalid) -> Feb 30 (invalid) -> Feb 29 (invalid for a 365-day year) -> Feb 28
Jan 31 + 2 months -> Mar 31
Jan 31 + 3 months -> Apr 31 (invalid) -> Apr 30
- So the function increases the month by n, and if the date is invalid, decreases the day by 1 until a valid date is reached.
- For me this appeared to be the best approach. Perhaps there are arguments for a slightly different approach, e.g. if a day is invalid, use the first day of the next month. There are pros and cons of whatever approach.
- Depending on the situation you might choose an arbitrary definition such as 31 days, or 30 days, or n working days. Maybe define a month as 365/12 = 30.416667 days, and add n of those.
Because this way adding 1 month to January 28, 29, 30 or 31 returns the same value.
Same if i do that on Oct 30,31 i get November 30.
That is not right (for me).

But again, that's what i think and i don't have a way to prove that it is the only or the RIGHT way to do that.
Both of us have our reasons to use different ways to add a month, but which one is the right way? i can't sleep because of that, hahaha.
Thank you for the link. I already did a lot of research about Gregorian Calendar and i understand the meaning of leap years, and why we have them. (didn't mean to sound rude or something, i actually appreciate that you gave me that link.)
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Help with adding a month (related to time in general, not only the code)

10 Dec 2018, 07:27

The duration of 1 month is not a fixed value. 1 month can last 28, 29, 30, or 31 days. That's why calculating with months is difficult.

If you develop your own method, you always should try to invert the calculation.

Code: Select all

D1 := 20180131
D2 := AddMonths(D1, 15)
D3 := AddMonths(D2, -15)
MsgBox, %D1% - %D2% - %D3%
should result in D3 being identical with D1. If not, the method shouldn't be called 'calculation'.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, vysmaty and 248 guests