Connecting Tech Pros Worldwide Help | Site Map

Subtract Hours from a Date

Lee
Guest
 
Posts: n/a
#1: Nov 16 '05
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


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Subtract Hours from a Date


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]
- mvp@spam.guard.caspershouse.com

"Lee" <latrotter@spamless.hotmail.com> wrote in message
news:uUmr5AKCFHA.208@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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
>[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Subtract Hours from a Date


Lee <latrotter@spamless.hotmail.com> wrote:[color=blue]
> I have a datetime field and want to subtract 24 (or any other number of)
> hours from it.
>
> How would I go about this?[/color]

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 - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Lee
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Subtract Hours from a Date


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]" <skeet@pobox.com> wrote in message
news:MPG.1c69ff3dbed68efd98bcd2@msnews.microsoft.c om...[color=blue]
> Lee <latrotter@spamless.hotmail.com> wrote:[color=green]
>> I have a datetime field and want to subtract 24 (or any other number of)
>> hours from it.
>>
>> How would I go about this?[/color]
>
> 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 - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#5: Nov 16 '05

re: Subtract Hours from a Date


Lee <latrotter@spamless.hotmail.com> wrote:[color=blue]
> 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))));[/color]

That seems a very longwinded way of doing:

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

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