469,311 Members | 2,501 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Subtract Hours from a Date

Lee
Hi All,

I have a datetime field and want to subtract 24 (or any other number of)
hours from it.

How would I go about this?

Thanks

Lee
Nov 16 '05 #1
4 20576
Lee,

What you want to do is create an instance of the TimeSpan class, and
then subtract that from your DateTime instance. You can also call the
AddDays, AddHours, etc, etc methods on the DateTime instance, passing
negative numbers to subtract the values from the DateTime. Note that these
methods return a new DateTime instance with the modified value, as opposed
to modifying the current value.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lee" <la*******@spamless.hotmail.com> wrote in message
news:uU*************@TK2MSFTNGP12.phx.gbl...
Hi All,

I have a datetime field and want to subtract 24 (or any other number of)
hours from it.

How would I go about this?

Thanks

Lee

Nov 16 '05 #2
Lee <la*******@spamless.hotmail.com> wrote:
I have a datetime field and want to subtract 24 (or any other number of)
hours from it.

How would I go about this?


Use DateTime.AddHours (-24) - and don't forget that that doesn't change
the value you call it on, it just returns a new DateTime.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Lee
Thanks, that was the right direction, This is the what I did to get it
working

DateTime dt =
DateTime.Now.Add(System.TimeSpan.FromHours(System. Convert.ToDouble((intHours*-1))));
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Lee <la*******@spamless.hotmail.com> wrote:
I have a datetime field and want to subtract 24 (or any other number of)
hours from it.

How would I go about this?


Use DateTime.AddHours (-24) - and don't forget that that doesn't change
the value you call it on, it just returns a new DateTime.

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

Nov 16 '05 #4
Lee <la*******@spamless.hotmail.com> wrote:
Thanks, that was the right direction, This is the what I did to get it
working

DateTime dt =
DateTime.Now.Add(System.TimeSpan.FromHours(System. Convert.ToDouble((i
ntHours*-1))));


That seems a very longwinded way of doing:

DateTime dt = DateTime.Now.AddHourse (-intHours);

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by John McGinty | last post: by
2 posts views Thread by Cyber Clone via DotNetMonster.com | last post: by
10 posts views Thread by Trapulo | last post: by
5 posts views Thread by Simon Dean | last post: by
12 posts views Thread by Assimalyst | last post: by
2 posts views Thread by Niyazi | last post: by
4 posts views Thread by YardDancer | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.