469,613 Members | 1,444 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

Substract number of month fron a date

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

someone have a easy solution ?
Thanks
Nov 15 '05 #1
7 16028
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" <de*******@sogescom.net> wrote in message
news:01****************************@phx.gbl...
I want to substract a number of month from a specific date.

someone have a easy solution ?
Thanks

Nov 15 '05 #2
developer <de*******@sogescom.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3
-----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
Nov 15 '05 #4
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" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ... developer <de*******@sogescom.net> wrote:
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.

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

Nov 15 '05 #5
Jon Skeet <sk***@pobox.com> wrote:
developer <de*******@sogescom.net> wrote:
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.


Look at Jay's answer - ignore this one. Sorry folks :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #6
Jay B. Harlow [MVP - Outlook] <Ja********@email.msn.com> wrote:
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 ;-)


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #7
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

Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Adam Knight | last post: by
3 posts views Thread by CBN Media | last post: by
29 posts views Thread by james | last post: by
3 posts views Thread by Jef Driesen | last post: by
2 posts views Thread by srusskinyon | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.