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
How to find date difference in C#?
To find the difference between two dates in C#, you can use the DateTime class along with the TimeSpan structure. The DateTime subtraction operation returns a TimeSpan object that represents the time difference between two dates.
Syntax
Following is the syntax for calculating date difference −
DateTime date1 = new DateTime(year, month, day); DateTime date2 = new DateTime(year, month, day); TimeSpan difference = date2 - date1;
The TimeSpan object provides various properties to access different units of the difference −
difference.Days // Total days difference.Hours // Hours component difference.Minutes // Minutes component difference.TotalHours // Total difference in hours difference.TotalMinutes // Total difference in minutes
Using TimeSpan to Calculate Date Difference
Example
using System;
class Program {
static void Main() {
DateTime date1 = new DateTime(2018, 09, 15);
DateTime date2 = new DateTime(2018, 09, 28);
Console.WriteLine("Date 1: {0}", date1.ToShortDateString());
Console.WriteLine("Date 2: {0}", date2.ToShortDateString());
TimeSpan difference = date2 - date1;
Console.WriteLine("Difference: {0} days", difference.Days);
Console.WriteLine("Total Hours: {0}", difference.TotalHours);
Console.WriteLine("Total Minutes: {0}", difference.TotalMinutes);
}
}
The output of the above code is −
Date 1: 9/15/2018 Date 2: 9/28/2018 Difference: 13 days Total Hours: 312 Total Minutes: 18720
Using DateTime.Subtract() Method
Alternatively, you can use the Subtract() method of the DateTime class to calculate the difference −
Example
using System;
class Program {
static void Main() {
DateTime startDate = new DateTime(2023, 1, 15, 10, 30, 0);
DateTime endDate = new DateTime(2023, 1, 20, 14, 45, 30);
Console.WriteLine("Start Date: {0}", startDate);
Console.WriteLine("End Date: {0}", endDate);
TimeSpan difference = endDate.Subtract(startDate);
Console.WriteLine("Days: {0}", difference.Days);
Console.WriteLine("Hours: {0}", difference.Hours);
Console.WriteLine("Minutes: {0}", difference.Minutes);
Console.WriteLine("Seconds: {0}", difference.Seconds);
Console.WriteLine("Total Days: {0:F2}", difference.TotalDays);
}
}
The output of the above code is −
Start Date: 1/15/2023 10:30:00 AM End Date: 1/20/2023 2:45:30 PM Days: 5 Hours: 4 Minutes: 15 Seconds: 30 Total Days: 5.18
Calculating Age Using Date Difference
Example
using System;
class Program {
static void Main() {
DateTime birthDate = new DateTime(1990, 5, 15);
DateTime currentDate = DateTime.Now;
TimeSpan ageDifference = currentDate - birthDate;
int ageInYears = (int)(ageDifference.TotalDays / 365.25);
Console.WriteLine("Birth Date: {0}", birthDate.ToShortDateString());
Console.WriteLine("Current Date: {0}", currentDate.ToShortDateString());
Console.WriteLine("Age: {0} years", ageInYears);
Console.WriteLine("Total Days Lived: {0}", (int)ageDifference.TotalDays);
}
}
The output of the above code is −
Birth Date: 5/15/1990 Current Date: 12/20/2024 Age: 34 years Total Days Lived: 12638
Comparison of Methods
| Method | Syntax | Use Case |
|---|---|---|
| Subtraction Operator | date2 - date1 |
Simple and direct calculation |
| Subtract() Method | date2.Subtract(date1) |
More explicit, method chaining |
| DateTime.Now | DateTime.Now - pastDate |
Calculate time elapsed from a past date |
Conclusion
Finding date differences in C# is accomplished using DateTime subtraction, which returns a TimeSpan object. The TimeSpan provides various properties like Days, Hours, and TotalDays to access different units of the time difference, making it easy to calculate elapsed time, age, or any duration between two dates.
