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?
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.
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.
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)).
You may take a look at the AddMinutes method:
var result = date1.AddMinutes(-15);
Use DateTime.Add(TimeSpan)
with a negative value.
Or use DateTime.AddDays()
, DateTime.AddMinutes()
, etc.
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