471,595 Members | 978 Online

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

For example:

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

Shouldn't this return 12/31/2005?

-Joe
Dec 21 '05 #1
11 17602
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.
"Joe" <jb*******@noemail.noemail> wrote in message
news:ez**************@TK2MSFTNGP10.phx.gbl...
Why doesn't AddMonths add the correct number of days for a given month

For example:

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

Shouldn't this return 12/31/2005?

-Joe

Dec 21 '05 #2
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

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:OA**************@TK2MSFTNGP14.phx.gbl...
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.
"Joe" <jb*******@noemail.noemail> wrote in message
news:ez**************@TK2MSFTNGP10.phx.gbl...
Why doesn't AddMonths add the correct number of days for a given month

For example:

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

Shouldn't this return 12/31/2005?

-Joe

Dec 21 '05 #3
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?

"Joe" <jb*******@noemail.noemail> wrote in message
news:ut**************@TK2MSFTNGP10.phx.gbl...
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

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:OA**************@TK2MSFTNGP14.phx.gbl...
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.
"Joe" <jb*******@noemail.noemail> wrote in message
news:ez**************@TK2MSFTNGP10.phx.gbl...
Why doesn't AddMonths add the correct number of days for a given month

For example:

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

Shouldn't this return 12/31/2005?

-Joe

Dec 21 '05 #4
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.

"Joe" <jb*******@noemail.noemail> wrote in message
news:ut**************@TK2MSFTNGP10.phx.gbl...
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

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:OA**************@TK2MSFTNGP14.phx.gbl...
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.
"Joe" <jb*******@noemail.noemail> wrote in message
news:ez**************@TK2MSFTNGP10.phx.gbl...
Why doesn't AddMonths add the correct number of days for a given month

For example:

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

Shouldn't this return 12/31/2005?

-Joe

Dec 21 '05 #5
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,

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Joe" <jb*******@noemail.noemail> wrote in message
news:ez**************@TK2MSFTNGP10.phx.gbl...
Why doesn't AddMonths add the correct number of days for a given month

For example:

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

Shouldn't this return 12/31/2005?

-Joe

Dec 21 '05 #6
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\\" +

of course you need a condition if its december.
Dec 21 '05 #7
Ignacio Machin ( .NET/ C# MVP ) wrote:
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,

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-
Dec 21 '05 #8
Joe <jb*******@noemail.noemail> wrote:
Why doesn't AddMonths add the correct number of days for a given month that

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Dec 21 '05 #9
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

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Dec 21 '05 #10
Rick Lones <Wr******@YcharterZ.net> wrote:
Ignacio Machin ( .NET/ C# MVP ) wrote:
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,

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Dec 21 '05 #11
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

"Joe" <jb*******@noemail.noemail> wrote in message
news:ez**************@TK2MSFTNGP10.phx.gbl...
Why doesn't AddMonths add the correct number of days for a given month

For example:

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

Shouldn't this return 12/31/2005?

-Joe

Dec 21 '05 #12

### This discussion thread is closed

Replies have been disabled for this discussion.