Substract number of month fron a date

  • Thread starter Thread starter developer
  • Start date Start date
D

developer

I want to substract a number of month from a specific date.

someone have a easy solution ?
Thanks
 
Developer,
The DataTime structure has an AddMonths method. Give it a negative # of
months to add.

Something like:
DateTime today = DateTime.Today;
today = today.AddMonths(-3);

Will subtract 3 months from today.

Hope this helps
Jay
 
developer said:
I want to substract a number of month from a specific date.

someone have a easy solution ?

I'd suggest creating a new DateTime using the same fields as the
original date, but subtracting the number of months appropriately.
However, you should consider cases like: "What does March 30th -1 month
mean?" and deal with them appropriately.
 
-----Original Message-----
I want to substract a number of month from a specific date.

someone have a easy solution ?
Thanks
.

Check out the DateTime class in .Net class library
 
Jon,
However, you should consider cases like: "What does March 30th -1 month
mean?" and deal with them appropriately.
The DateTime.AddMonths with a negative number takes care of that!

However its not symmetrical, March 30th - 1 month, that date +1 month is not
March 30th. Which is always a problem with date arithmetic ;-)

Just a thought
Jay
 
Jon Skeet said:
I'd suggest creating a new DateTime using the same fields as the
original date, but subtracting the number of months appropriately.
However, you should consider cases like: "What does March 30th -1 month
mean?" and deal with them appropriately.

Look at Jay's answer - ignore this one. Sorry folks :)
 
Jay B. Harlow said:
The DateTime.AddMonths with a negative number takes care of that!

However its not symmetrical, March 30th - 1 month, that date +1 month is not
March 30th. Which is always a problem with date arithmetic ;-)

And not the only one.

Over time, I've worked out that the following are just some of the
nightmare worlds it's best not to delve too far into:

o Dates and times, with timezones etc
o Unicode surrogates and the other text oddities (capitalisation being
culture-dependent, etc)
o Telephone numbers (formatting, parsing, generally handling)

Basically anything to do with internationalisation :)
 
Hi Jon
Over time, I've worked out that the following are just some of the
nightmare worlds it's best not to delve too far into:

o Dates and times, with timezones etc

One thing that has always intrigued me (as an antipodean) is how Americans
(apologies if you are not from the USA) can ignore timezones. After all you
have three across the USA, and many applications must have to deal with
this.

I was suprised the the .NET framework decided only to support the local
computer's timezone. With outsourcing across the world being popular these
days, timezone support is a necessity. Fortunately there are some freeware
classes out there that support timezones on Windows at least. But there are
no user controls to my knowledge that will correctly validate local times.
For instance 2003-03-16 02:30:00 is ambiguous in NZ, as that day we went off
dayliight saving and had two of them. Similarly the time 2003-10-05 02:30:00
will not occur, since we go back on to daylight saving that day.

Around 1993 I built such controls for Informix 4GL under Unix. I thought the
world would have caught up by now!

I think this is an ommission from the framework that should be fixed as soon
as possible.

Regards

Ron
 
Back
Top