bug of date class... add months or years generated wrong result

hi this code generate a false result…

dim d as new date
'… now d is 30/06/2013 (if format is dd/mm/yyyy) or 06/30/2013 (if format is mm/dd/yyyy)


'… now i think that d will be 31/07/2013 or 07/31/2013 because i add 1 month …
'…but d is 30/07/2013 or 07/30/2013 … :frowning: and i haven’t add 1 month but 30 days…
'…this is big error of class or not ???
'and if i write…


'…the result is that d= 28/07/2023 or 07/28/2023

'…this means that 1 month =30 days but this is not true because there are months with 31 days and with 28 days
'… and that 10 years are 3650 days and also this is not true

Incrementing d.Month does just that, it adds one month. The Day remains unchanged unless its value is not valid for the month, in which case the date will be adjusted to a correct value. (So be careful about simply changing the Month if the Day is greater than 28.)

And anyway, if the date is 2013-06-30 and you add one month, it should be 2013-07-30, not 2013-07-31.

And if the date is 2013-07-30, then

d.year = d.year + 10

yields 2023-07-30, as it should.

sorry but i think that if i add 1 month to a date with day set to the last day of month (x) i must have in return a date equal to the last day of month(x+1).
otherwise the month property is unusable… we can use day property like d.day=d.day+30xMonths…

and if i want add 30 or 40 or xx days to my date i must increment d.day property .

While “Last day of the Month” is a reasonable Feature Request (via the Feedback app), it just doesn’t work that way. And I would expect it to leave the day alone if all I ask is to increment the month. It would be very bad if the functionality changed simply because the day happened to be the last day of the month. What you’re asking for should really be a separate method in the Date class and not based on simply changing the month property. File a FR.


Increments ONLY the month attribute, and will affect the YEAR attribute if d.month becomes “13”
but it has NO effect on the day attribute, UNLESS the NEW month has less days than the current value of d.days

For the most part it affects the attribute you address, and any higher priority attributes if they roll over

' becomes 02/28/2013 since Feb does not have 31 days in it

but if the current month has 30 days… incrementing MONTH does not move to 31 days automatically, nor should it

So it did not produce a wrong result, it simply produced a result you were not expecting.

Since “a month” is a VERY variable unit of time (sometimes 30 days sometimes 31 sometimes 28 or 29) what “adding a month” means is subject to interpretation.

If its the 30th of the January & we get together & at the end of the meeting I say “see you next month” what would that even mean ?
There’s never a 30th of February
So if I showed up on the 28th you might be surprised since it wasn’t a moth from our last meeting.
If its the 15th of the January & we get together & at the end of the meeting I say “see you next month” that more than likely means “see you again on the the 15th of February”
Thats entirely plausible.

I’d avoid “adding one month” and being surprised because its not what you expected - but might be what someone else expected.
Add the number of days you mean instead.

Unfortunately, this is where it becomes non-intuitive. The date here will be 03/03/2013. There is a certain logic to it - the day was 3 days past the end of the month, so it does come up with “a” correct answer. Just not the one you might expect. I don’t have a problem with the way it works, it seems the “most correct” of the various things you could do in this situation, if not the most intuitive.

ok tnx to everyone…
i think that if i add 1,2,3 months to month property the compiler must check if the next 1…or 2… months from myDate are months with 28,30,31 days and add the correct number of days… to myDate
like :
15 january + 2months must return 15 march
but evidently i’m wrong way…

Tim… try it… it comes up 02/28… surprised me too it did.

I did. came up 03/03/2013 as I expected.

Just to be sure, I copy/pasted your code. Still 03/03/2013.

The following code should find the end of the next month. I put it in a button action event just to test.

[code] Dim d As New Date
d.day = 31
d.Month = 1
d.year = 2013

d.day = d.day + 40
d.day = 1
d.day = d.day - 1
MsgBox d.ShortDate

However my message box shows 31/12/2012? So I put a break point on the d.day = d.day + 40 line & stepped through every thing looked right & I got a date of 28/02/2013.

Can anyone explain this?

Windows 7 x64 8GB RAM

That is quite interesting… because for me… it comes up 02/28
I would not have replied as such if I had not seen that result…

What OS? I’m on OSX 10.8.4

Quite possibly the difference in how the OS routines we use handle it

Yeah, I’m using Windows 7.