Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I subtract 6 hour from the current time? [duplicate]

Tags:

c#

.net

datetime

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?

like image 439
markzzz Avatar asked Dec 07 '12 08:12

markzzz


People also ask

How do I subtract hours from a timestamp?

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.

How do you subtract time?

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

How do I subtract hours from a date in Excel?

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

How do I subtract hours in C#?

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.


2 Answers

You can use AddHours, it will subtract hours if negative number is passed.

DateTime dt1 = dt.AddHours(-6); 
like image 102
Adil Avatar answered Oct 02 '22 00:10

Adil


You can use negative values to subtract time

myTime.AddHours(-6); 
like image 32
Wim Ombelets Avatar answered Oct 01 '22 23:10

Wim Ombelets