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
C# Program to get the difference between two dates in seconds
In C#, you can calculate the difference between two dates in seconds using the TimeSpan structure. When you subtract one DateTime from another, it returns a TimeSpan object that represents the time interval between them.
Syntax
Following is the syntax for calculating date difference in seconds −
DateTime date1 = new DateTime(year, month, day, hour, minute, second); DateTime date2 = new DateTime(year, month, day, hour, minute, second); TimeSpan difference = date2 - date1; double seconds = difference.TotalSeconds;
Using DateTime Subtraction
The most straightforward approach is to subtract one DateTime from another, which returns a TimeSpan. The TotalSeconds property gives you the total difference in seconds −
using System;
public class Demo {
public static void Main() {
DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20);
DateTime date2 = new DateTime(2018, 7, 15, 11, 14, 25);
TimeSpan ts = date2 - date1;
Console.WriteLine("Date 1: " + date1);
Console.WriteLine("Date 2: " + date2);
Console.WriteLine("Difference in seconds: " + ts.TotalSeconds);
}
}
The output of the above code is −
Date 1: 7/15/2018 8:15:20 AM Date 2: 7/15/2018 11:14:25 AM Difference in seconds: 10745
Using DateTime.Now for Current Time
You can also calculate the difference between a specific date and the current time using DateTime.Now −
using System;
public class Demo {
public static void Main() {
DateTime pastDate = new DateTime(2024, 1, 1, 0, 0, 0);
DateTime currentDate = DateTime.Now;
TimeSpan difference = currentDate - pastDate;
Console.WriteLine("Past Date: " + pastDate);
Console.WriteLine("Current Date: " + currentDate);
Console.WriteLine("Difference in seconds: " + difference.TotalSeconds);
Console.WriteLine("Difference in days: " + difference.TotalDays.ToString("F2"));
}
}
The output will vary based on when you run the code, but it will be similar to −
Past Date: 1/1/2024 12:00:00 AM Current Date: 12/19/2024 2:30:45 PM Difference in seconds: 30326445 Difference in days: 351.10
Handling Negative Differences
When the first date is later than the second date, the result will be negative. You can use Math.Abs() to get the absolute difference −
using System;
public class Demo {
public static void Main() {
DateTime date1 = new DateTime(2018, 7, 15, 11, 14, 25);
DateTime date2 = new DateTime(2018, 7, 15, 08, 15, 20);
TimeSpan difference = date2 - date1;
double absoluteSeconds = Math.Abs(difference.TotalSeconds);
Console.WriteLine("Date 1: " + date1);
Console.WriteLine("Date 2: " + date2);
Console.WriteLine("Raw difference: " + difference.TotalSeconds + " seconds");
Console.WriteLine("Absolute difference: " + absoluteSeconds + " seconds");
}
}
The output of the above code is −
Date 1: 7/15/2018 11:14:25 AM Date 2: 7/15/2018 8:15:20 AM Raw difference: -10745 seconds Absolute difference: 10745 seconds
TimeSpan Properties
| Property | Description |
|---|---|
| TotalSeconds | Gets the total difference in seconds (as double) |
| TotalMinutes | Gets the total difference in minutes |
| TotalHours | Gets the total difference in hours |
| TotalDays | Gets the total difference in days |
Conclusion
Calculating the difference between two dates in seconds in C# is accomplished by subtracting one DateTime from another to get a TimeSpan, then using the TotalSeconds property. This method works for any two DateTime objects and returns a double representing the total seconds difference.
