473,320 Members | 1,952 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,320 software developers and data experts.

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 21006
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John McGinty | last post by:
Hello Chaps, Just a little sounding on removing out of hours from some datetime date that I have. Basically we have a helpdesk that logs calls when they are entered and when they are resolved....
4
by: S. van Beek | last post by:
Dear reader, By a Date field with Now() as default value the content of the field is date plus time. As I need a filter in a query on date only (excluding time) I invented the following...
2
by: Cyber Clone via DotNetMonster.com | last post by:
Hello All, i want to know how to date different in csharp. i can datediff in vb6. is there any simple command for date different (CSharp)? thanks Cyber Clone
10
by: Trapulo | last post by:
Why Now.Date.Subtract(New Date(2000, 1, 1)).Days returns 731529?? It is a too big value a think! I aspect something as 1030-1100....
5
by: Simon Dean | last post by:
Probably being a little thick here, but when you subtract one date away from another, how do you convert the resultant value into a number of days... I guess I could easily / 60 / 60 / 24... but...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
2
by: Niyazi | last post by:
Hi everyone, I have a sql table that has 5 column as: cl1Month - cl1_3Month - cl3_6Month - cl6_12Month - clMoreThan12Month Now I have to date and I have to find the differences and check as...
4
by: YardDancer | last post by:
Dear All, I am upgrading my skills from VB 6.0 to VB.Net. They say you can do all date and time calculations using the properties and methods of DateTime structure withour recourse to the...
2
by: Benniit | last post by:
I'm using vb.net 2008 and sqlserver 2008 and I want subtract a date from a specified date. Example: I want to subtract Commencedate from Today's Date but when I used datediff it gave me (argument...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.