473,395 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

DateTime.AddMonths bug?

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
Dec 21 '05 #1
11 17920
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
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

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


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
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



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
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



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,
30).AddMonths(1) ?
Answer: you get 2/28/2005


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
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

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\\" +
DateTime.Now.Year).AddDays(-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,
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-
Dec 21 '05 #8
Joe <jb*******@noemail.noemail> wrote:
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.

--
Jon Skeet - <sk***@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
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
DateTime.AddDays( Qty_Of_day_Of_Next_month)

--
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,
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.


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
If replying to the group, please do not mail me too
Dec 21 '05 #11
Joe
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
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

Dec 21 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Max M | last post by:
# -*- coding: latin-1 -*- """ I am currently using the datetime package, but I find that the design is oddly asymmetric. I would like to know why. Or perhaps I have misunderstood how it...
2
by: ThomasR | last post by:
I've done some examination of the DateTime implementation (stepped through Disassembly in debug mode) and find it horribly inefficient in framework 1.0. For example, the internal DatePart...
5
by: Abdolhosein Vakilzadeh Ebrahimi | last post by:
Is there any way to construct a System.DateTime which doesn't check year, month and day to be in correct range== no validation? Thanks A.V.Ebrahimi
15
by: Fritz Switzer | last post by:
I'd like to have a string assigned the value of a DateTime.AddMinutes(amount) so that the string is formatted in "HH:MM" format. For example: DateTime.Now.AddMinutes(30) returns "00:30" ...
6
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
6
by: Helen | last post by:
I have an array being passed as a parameter to Crystal 9 (using vb .net in VS 2002). The listbox containing the dataset output at runtime shows DateTime format, but I want to only show mm/dd/yyyy...
9
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
3
by: Lars Schouw | last post by:
I would like to to add a time span "1Y 2M 3W 20D" entered as a string to a date time how do I do that? Lars
1
by: bbawa1 | last post by:
Hi, I have a column of datatype DateTime. I need to retrieve data from last 14 days old. I am doing like this but it doesn't work. Select reveiveddate from table1 where receiveddate =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.