Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

DateTime.AddMonths bug?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joe - 21 Dec 2005 17:07 GMT
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
Peter Rilling - 21 Dec 2005 17:15 GMT
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.

> Why doesn't AddMonths add the correct number of days for a given month
> that is being added?
[quoted text clipped - 6 lines]
>
> -Joe
Joe - 21 Dec 2005 17:30 GMT
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

> 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.
[quoted text clipped - 14 lines]
>>
>> -Joe
Peter Rilling - 21 Dec 2005 17:40 GMT
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?

> Hi Peter,
> That's a good point but what if you ask someone what a month from Jan-31
[quoted text clipped - 25 lines]
>>>
>>> -Joe
Peter Rilling - 21 Dec 2005 17:43 GMT
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 Peter,
> That's a good point but what if you ask someone what a month from Jan-31
[quoted text clipped - 25 lines]
>>>
>>> -Joe
Ignacio Machin ( .NET/ C# MVP ) - 21 Dec 2005 18:38 GMT
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,

Signature

Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

> Why doesn't AddMonths add the correct number of days for a given month
> that is being added?
[quoted text clipped - 6 lines]
>
> -Joe
Rick Lones - 21 Dec 2005 19:06 GMT
> Hi,
>
[quoted text clipped - 5 lines]
> 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-
Jon Skeet [C# MVP] - 21 Dec 2005 19:19 GMT
> > Hi,
> >
[quoted text clipped - 11 lines]
>
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Ben Dewey - 21 Dec 2005 19:01 GMT
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.
Jon Skeet [C# MVP] - 21 Dec 2005 19:06 GMT
> Why doesn't AddMonths add the correct number of days for a given month that
> is being added?
[quoted text clipped - 4 lines]
>
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Ignacio Machin ( .NET/ C# MVP ) - 21 Dec 2005 19:15 GMT
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)

Signature

Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Joe - 21 Dec 2005 19:53 GMT
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

> Why doesn't AddMonths add the correct number of days for a given month
> that is being added?
[quoted text clipped - 6 lines]
>
> -Joe

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.