Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtracting from a 'DateTime'

Tags:

c#

datetime

I want to subtract from a DateTime. Example:

date1 = 13/01/2004 12:20:00
result = Subtract(date1-15);

Expected output:

13/01/2004 12:05:00

How do I do this?

like image 921
Ravi Avatar asked May 19 '10 16:05

Ravi


People also ask

Can you subtract datetime Python?

For adding or subtracting Date, we use something called timedelta() function which can be found under the DateTime class. It is used to manipulate Date, and we can perform arithmetic operations on dates like adding or subtracting.

Can you subtract two datetime objects?

It is allowed to subtract one datetime object from another datetime object. The resultant object from subtraction of two datetime objects is an object of type timedelta.

Can you subtract datetime in SQL?

The answer is yes. The subtract operation can be performed by the subtract operator (-) as: datetime1 - datetime2: Returning a DATETIME value calculated as CONVERT(DATETIME, CONVERT(NUMERIC(18,9),datetime1) - CONVERT(NUMERIC(18,9),datetime2)).


2 Answers

You may take a look at the AddMinutes method:

var result = date1.AddMinutes(-15);
like image 93
Darin Dimitrov Avatar answered Oct 30 '22 09:10

Darin Dimitrov


Use DateTime.Add(TimeSpan) with a negative value.

Or use DateTime.AddDays(), DateTime.AddMinutes(), etc.

like image 36
CaffGeek Avatar answered Oct 30 '22 08:10

CaffGeek