Possible Duplicate:
c#: whats the easiest way to subtract time?
Suppose I have this time :
07/12/2012 - 00:30:45
I'd like to subtract 6 hour, such as 06/12/2012 - 18:30:45. I see there are .AddHours() but not SubHours(). How can I do it?
To subtract hours from a given timestamp, we are going to use the datetime and timedelta classes of the datetime module. Step 1: If the given timestamp is in a string format, then we need to convert it to the datetime object. For that we can use the datetime. strptime() function.
To subtract time, subtract the minutes then subtract the hours. Since we can't have negative minutes, add 60 to the minutes and subtract 1 from the hours (60 minutes = 1 hour).
To subtract time, type in =B2-B1, and it'll return the elapsed time. The answer is displayed as an AM time, so to change that, right click and select Format Cells and change it to h:mm. It'll return the answer in time format (7:35, or 7 hours and 35 minutes).
Add(new TimeSpan(-2,0,0)); DateTime original = new DateTime(year, month, day, 17, 30, 0); DateTime updated = original. Add(new TimeSpan(0,-45,0)); Or you can also use the DateTime. Subtract(TimeSpan) method analogously.
You can use AddHours, it will subtract hours if negative number is passed.
DateTime dt1 = dt.AddHours(-6);
You can use negative values to subtract time
myTime.AddHours(-6);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With