Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
DateTime.Subtract() Method in C#
The DateTime.Subtract() method in C# is used to subtract either a DateTime or a TimeSpan from the current DateTime instance. This method has two overloads: one subtracts a DateTime and returns the time difference as a TimeSpan, while the other subtracts a TimeSpan and returns a new DateTime.
Syntax
Following are the two overloads of the DateTime.Subtract() method −
public TimeSpan Subtract(DateTime value); public DateTime Subtract(TimeSpan value);
Parameters
-
value − Either a
DateTimeinstance to subtract from the current date, or aTimeSpanrepresenting the duration to subtract.
Return Value
-
When subtracting a
DateTime: Returns aTimeSpanrepresenting the difference between the two dates. -
When subtracting a
TimeSpan: Returns a newDateTimethat is earlier by the specified duration.
Using DateTime.Subtract() with DateTime
When you subtract one DateTime from another, the result is a TimeSpan representing the duration between them −
using System;
public class Demo {
public static void Main() {
DateTime d1 = new DateTime(2019, 10, 10, 8, 10, 40);
DateTime d2 = new DateTime(2017, 11, 6, 8, 10, 40);
Console.WriteLine("Date 1 = " + d1);
Console.WriteLine("Date 2 = " + d2);
TimeSpan res = d1.Subtract(d2);
Console.WriteLine("TimeSpan between two dates = {0}", res);
Console.WriteLine("Total days difference: {0}", res.TotalDays);
}
}
The output of the above code is −
Date 1 = 10/10/2019 8:10:40 AM Date 2 = 11/6/2017 8:10:40 AM TimeSpan between two dates = 703.00:00:00 Total days difference: 703
Using DateTime.Subtract() with TimeSpan
When you subtract a TimeSpan from a DateTime, you get a new DateTime that is earlier by the specified duration −
using System;
public class Demo {
public static void Main() {
DateTime d = new DateTime(2019, 10, 10, 8, 10, 40);
TimeSpan t = new TimeSpan(1, 10, 10, 12); // 1 day, 10 hours, 10 minutes, 12 seconds
Console.WriteLine("Original Date = " + d);
Console.WriteLine("TimeSpan to subtract = " + t);
DateTime res = d.Subtract(t);
Console.WriteLine("Result after subtraction = {0}", res);
}
}
The output of the above code is −
Original Date = 10/10/2019 8:10:40 AM TimeSpan to subtract = 1.10:10:12 Result after subtraction = 10/8/2019 10:00:28 PM
Practical Example - Age Calculation
Here's a practical example that calculates someone's age using DateTime.Subtract() −
using System;
public class AgeCalculator {
public static void Main() {
DateTime birthDate = new DateTime(1990, 5, 15);
DateTime currentDate = DateTime.Now;
TimeSpan age = currentDate.Subtract(birthDate);
int ageInYears = (int)(age.TotalDays / 365.25);
Console.WriteLine("Birth Date: " + birthDate.ToShortDateString());
Console.WriteLine("Current Date: " + currentDate.ToShortDateString());
Console.WriteLine("Age in days: " + (int)age.TotalDays);
Console.WriteLine("Age in years: " + ageInYears);
}
}
Comparison of DateTime.Subtract() Overloads
| Overload | Parameter | Return Type | Purpose |
|---|---|---|---|
| Subtract(DateTime) | DateTime | TimeSpan | Find the duration between two dates |
| Subtract(TimeSpan) | TimeSpan | DateTime | Go back in time by a specific duration |
Conclusion
The DateTime.Subtract() method provides two essential operations: calculating the time difference between two dates and going back in time by a specified duration. Use the first overload for finding intervals and the second for date arithmetic operations.
