DateTime.AddMonths bug?

  • Thread starter Thread starter Joe
  • Start date Start date
J

Joe

Why doesn't AddMonths add the correct number of days for a given month that
is being added?

For example:

new DateTime(2005, 11, 30).AddMonths(1) returns 12/30/2005.

Shouldn't this return 12/31/2005?

-Joe
 
Well, I don't know about the logic of the structure, but if you ask a friend
what the date is one month from today, they would say January 21. Seems
reasonable that DateTime would respond the same.

The problem with datetime calculations is, how do you know how many days to
add? Do you work with the number of days in the current month, or next
month. Hard to make that determination.
 
Hi Peter,
That's a good point but what if you ask someone what a month from Jan-31 is?

I guess you're right. It could be thought of either way.

I'll probably have to write something myself.

Thanks,
Joe
 
That is also interesting. Or what if someone asks one month (or even one
year) from Feb 29.

Oh why didn't they invent a calendar that has the same number of days each
month?
 
By the way, I bet if you asked a thousand people what a month from Jan-31
is, I bet you would get both Feb 30 and Mar 1 as the answer, which I guess
makes writing an algorithm difficult.
 
Hi,

You are adding which month? and with what criteria?

I find the selected behavior increment the month in 1 is ok.

Now, a good question, what happen when you do new DateTime(2005, 1,
30).AddMonths(1) ?
Answer: you get 2/28/2005




cheers,
 
If you want the last day of the month its better take one day off the month
plus.

ie. To get the last day of this month do

DateTime lastday = DateTime.Parse((DateTime.Now.Month+1) + "\\1\\" +
DateTime.Now.Year).AddDays(-1);

of course you need a condition if its december.
 
Ignacio said:
Hi,

You are adding which month? and with what criteria?

I find the selected behavior increment the month in 1 is ok.

Now, a good question, what happen when you do new DateTime(2005, 1,
30).AddMonths(1) ?
Answer: you get 2/28/2005

And if you do new DateTime(2005,2,1).AddMonths(1) you get 3/1/2005. So
apparently it simply increments the month "digit" unless that causes "overflow",
in which case it backs up to the last day of the resulting month.

Reasonable enough, but not exactly obvious - if that is even the algorithm.

-rick-
 
Joe said:
Why doesn't AddMonths add the correct number of days for a given month that
is being added?

For example:

new DateTime(2005, 11, 30).AddMonths(1) returns 12/30/2005.

Shouldn't this return 12/31/2005?

Why would it? It's thirty days into November, plus a month. For me,
that should return thirty days into December.

It gets more tricky when you add a month from the 30th of January, and
unfortunately the behaviour isn't actually specified in the docs as far
as I can see. I'd expect it to give February 28th (or 29th for a leap
year), that being the closest to February 30th which is actually
possible. I don't see that there's any ambiguity in your situation
though.
 
Hi,

It gets more tricky when you add a month from the 30th of January, and
unfortunately the behaviour isn't actually specified in the docs as far
as I can see. I'd expect it to give February 28th (or 29th for a leap
year), that being the closest to February 30th which is actually
possible. I don't see that there's any ambiguity in your situation
though.


I agree with you , the current behavior is the one I was expecting, the OP
was thinking in DateTime.AddDays( Qty_Of_day_Of_Current_month) or maybe
DateTime.AddDays( Qty_Of_day_Of_Next_month)
 
Rick Lones said:
And if you do new DateTime(2005,2,1).AddMonths(1) you get 3/1/2005. So
apparently it simply increments the month "digit" unless that causes "overflow",
in which case it backs up to the last day of the resulting month.

Reasonable enough, but not exactly obvious - if that is even the algorithm.

It seems very reasonable to me, and probably what I'd decide on if I
were trying to design it. It's just a shame it's not actually
documented as far as I can see. Silly thing to omit, really.
 
WOW! I knew this would get some interesting responses.

I asked several people today the following questions
1 - What is the date 1 month from today. Everyone answered 1/21
2 - What is the date 1 month from 1/31. Some answered 3/2 and others said
3/3. One said 2/28
3 - What is one month from 11/30 - I got a split - 12/30 & 12/31. (This
question was asked last so some people started thinking)

It's interesting how people think. In all cases when asking what is the date
from any date that is not the end of the month everyone answers the same day
in the next month 1/15 -> 2/15

I understand how AddMonths work I just wasn't exactly sure if that was
intended.

Anyway, it was an interesting discussion.
-Joe
 
Back
Top