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# difference in milliseconds between two DateTime
In C# programming, you often need to calculate the time difference between two DateTime objects. The most efficient way to get the difference in milliseconds is by using the TimeSpan structure and its TotalMilliseconds property.
Syntax
Following is the syntax for calculating milliseconds difference between two DateTime objects −
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 milliseconds = difference.TotalMilliseconds;
Using TimeSpan to Calculate Milliseconds Difference
When you subtract one DateTime from another, the result is a TimeSpan object that represents the time interval. The TotalMilliseconds property returns the total number of milliseconds in that interval as a double value −
Example
using System;
public class Demo {
public static void Main() {
DateTime date1 = new DateTime(2018, 8, 11, 08, 15, 20);
DateTime date2 = new DateTime(2018, 8, 11, 11, 14, 25);
TimeSpan ts = date2 - date1;
Console.WriteLine("Date1: " + date1);
Console.WriteLine("Date2: " + date2);
Console.WriteLine("Difference in Milliseconds: " + ts.TotalMilliseconds);
Console.WriteLine("Difference in Seconds: " + ts.TotalSeconds);
Console.WriteLine("Difference in Minutes: " + ts.TotalMinutes);
}
}
The output of the above code is −
Date1: 8/11/2018 8:15:20 AM Date2: 8/11/2018 11:14:25 AM Difference in Milliseconds: 10745000 Difference in Seconds: 10745 Difference in Minutes: 179.0833333333333
Working with Current DateTime
You can also calculate the time difference between the current time and a specific date using DateTime.Now −
Example
using System;
public class TimeDifferenceDemo {
public static void Main() {
DateTime startTime = new DateTime(2024, 1, 1, 0, 0, 0);
DateTime currentTime = DateTime.Now;
TimeSpan timeDifference = currentTime - startTime;
Console.WriteLine("Start Time: " + startTime);
Console.WriteLine("Current Time: " + currentTime);
Console.WriteLine("Milliseconds since start: " + timeDifference.TotalMilliseconds);
Console.WriteLine("Days since start: " + timeDifference.TotalDays);
}
}
The output of the above code is −
Start Time: 1/1/2024 12:00:00 AM Current Time: 12/19/2024 2:30:15 PM Milliseconds since start: 30686415000000 Days since start: 353.1042534722222
TimeSpan Properties for Different Time Units
| Property | Description | Return Type |
|---|---|---|
| TotalMilliseconds | Total milliseconds in the time span | double |
| TotalSeconds | Total seconds in the time span | double |
| TotalMinutes | Total minutes in the time span | double |
| TotalHours | Total hours in the time span | double |
| TotalDays | Total days in the time span | double |
Handling Negative Time 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 −
Example
using System;
public class NegativeDifferenceDemo {
public static void Main() {
DateTime laterDate = new DateTime(2024, 12, 31, 23, 59, 59);
DateTime earlierDate = new DateTime(2024, 1, 1, 0, 0, 0);
TimeSpan difference1 = earlierDate - laterDate; // Negative
TimeSpan difference2 = laterDate - earlierDate; // Positive
Console.WriteLine("Negative difference: " + difference1.TotalMilliseconds);
Console.WriteLine("Positive difference: " + difference2.TotalMilliseconds);
Console.WriteLine("Absolute difference: " + Math.Abs(difference1.TotalMilliseconds));
}
}
The output of the above code is −
Negative difference: -31535999000 Positive difference: 31535999000 Absolute difference: 31535999000
Conclusion
Calculating the milliseconds difference between two DateTime objects in C# is straightforward using TimeSpan subtraction and the TotalMilliseconds property. This approach provides precise time measurements and can handle both positive and negative differences depending on the order of subtraction.
