Calculate difference between two dates in C#
This post will discuss how to find the time difference between two DateTime objects in C#.
If we subtract two DateTime objects in C#, we’ll get a TimeSpan object representing a time interval. The following code example prints a string representation of the TimeSpan object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; public class Example { public static void Main() { DateTime now = DateTime.Now; DateTime then = DateTime.Now.AddDays(-7); TimeSpan ts = now - then; // or, use `now.Subtract(then)` Console.WriteLine(ts); } } /* Output: 6.23:59:59.9992191 */ |
The TimeSpan object exposes several useful properties such as TotalDays, TotalHours, TotalMinutes, TotalSeconds, TotalMilliseconds, to get the total number of days, hours, minutes, seconds, milliseconds, respectively. The examples of each of these properties are demonstrated below:
1. Using TimeSpan.TotalDays() method
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; public class Example { public static void Main() { DateTime now = DateTime.Now; DateTime then = DateTime.Now.AddDays(-7); TimeSpan ts = now.Subtract(then); double diff = ts.TotalDays; Console.WriteLine(diff); } } /* Output: 6.999999990922453 */ |
2. Using TimeSpan.TotalHours() method
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; public class Example { public static void Main() { DateTime now = DateTime.Now; DateTime then = DateTime.Now.AddDays(-7); TimeSpan ts = now.Subtract(then); Console.WriteLine(ts.TotalHours); } } /* Output: 167.99999975805557 */ |
3. Using TimeSpan.TotalMinutes() method
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; public class Example { public static void Main() { DateTime now = DateTime.Now; DateTime then = DateTime.Now.AddDays(-7); TimeSpan ts = now.Subtract(then); Console.WriteLine(ts.TotalMinutes); } } /* Output: 10079.999985743334 */ |
4. Using TimeSpan.TotalSeconds() method
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; public class Example { public static void Main() { DateTime now = DateTime.Now; DateTime then = DateTime.Now.AddDays(-7); TimeSpan ts = now.Subtract(then); Console.WriteLine(ts.TotalSeconds); } } /* Output: 604799.9992224 */ |
5. Using TimeSpan.TotalMilliseconds() method
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; public class Example { public static void Main() { DateTime now = DateTime.Now; DateTime then = DateTime.Now.AddDays(-7); TimeSpan ts = now.Subtract(then); Console.WriteLine(ts.TotalMilliseconds); } } /* Output: 604799999.2238 */ |
The TimeSpan object also offers several other useful properties such as Days, Hours, Minutes, Seconds, Milliseconds that returns the day component, hour component, minute component, second component, milliseconds component, respectively. The following code example demonstrates its usage.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; public class Example { public static void Main() { DateTime now = DateTime.Now; DateTime then = DateTime.Now.AddDays(-7); TimeSpan ts = now.Subtract(then); Console.WriteLine("{0} Days, {1} Hours, {2} Minutes, {3} Seconds, {4} Milliseconds", ts.Days, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds); } } /* Output: 6 Days, 23 Hours, 59 Minutes, 59 Seconds, 999 Milliseconds */ |
That’s all about calculating the difference between two dates in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)