How to Calculate the Difference between Two Dates?
Here you will learn how to calculate the difference between two dates in C#.
The difference between two dates can be calculated in C# by using the substraction operator -
or the DateTime.Subtract()
method.
The following example demonstrates getting the time interval between two dates using the -
operator.
// Declare two dates
var prevDate = new DateTime(2021, 7, 15); //15 July 2021
var today = DateTime.Now;
//get difference of two dates
var diffOfDates = today - prevDate;
Console.WriteLine("Difference in Timespan: {0}", diffOfDates);
Console.WriteLine("Difference in Days: {0}", diffOfDates.Days);
Console.WriteLine("Difference in Hours: {0}", diffOfDates.Hours);
Console.WriteLine("Difference in Miniutes: {0}", diffOfDates.Minutes);
Console.WriteLine("Difference in Seconds: {0}", diffOfDates.Seconds);
Console.WriteLine("Difference in Milliseconds: {0}", diffOfDates.Milliseconds);
Console.WriteLine("Difference in Ticks: {0}", diffOfDates.Ticks);
In the above example, the -
operator substract prevDate
from today
and return the result as a TimeSpan
object. This Timespan
object can be used to get the difference in days, hours, minutes, seconds, milliseconds, and ticks using the returned object. Note that the DateTime object in the above example represents the times in the same time zone.
The -
operator can also be using to deduct TimeSpan
value from DateTime
, as shown below.
DateTime dt1 = new DateTime(2020, 4, 11, 4, 0, 12); //11 April 2020 4:00:12
DateTime dt2 = new DateTime(2020, 5, 11, 5, 20, 28); //11 May 2020 5:20:28
DateTime dt3 = new DateTime(2020, 6, 11); //11 June 2020 0:00:00
TimeSpan interval = new TimeSpan(2, 14, 18); // 02:14:15
TimeSpan diff1 = dt2 - dt1; //DateTime - DateTime
TimeSpan diff2 = dt3 - dt2; //Date - DateTime
DateTime diff3 = dt3 - interval; //Date - TimeSpan
DateTime diff4 = dt2 - interval; //DateTime - TimeSpan
//interval.Subtract(dt3); // error
Console.WriteLine("{0} - {1} = {2}", dt2, dt1, diff1);
Console.WriteLine("{0} - {1} = {2}", dt3, dt2, diff2);
Console.WriteLine("{0} - {1} = {2}", dt3, interval, diff3);
Console.WriteLine("{0} - {1} = {2}", dt2, interval, diff4);
05/11/2020 05:20:28 - 04/11/2020 04:00:12 = 30.01:20:16 06/11/2020 00:00:00 - 05/11/2020 05:20:28 = 30.18:39:32 06/11/2020 00:00:00 - 02:14:18 = 06/10/2020 21:45:42 05/11/2020 05:20:28 - 02:14:18 = 05/11/2020 03:06:10
DateTime.Subtract()
The DateTime.Subtract()
method returns the value by subtracting the specified date or timespan (duration) from the date instance.
DateTime.Subtract(Datetime)
returns a new time interval that subtracts the specified Datetime from the current instance.
DateTime.Subtract(TimeSpan)
returns a new DateTime that subtracts the specified time interval (TimeSpan value) from the current instance.
The following example demonstrates getting difference of two dates using the DateTime.Subtract()
method:
// Declare two dates
var prevDate = new DateTime(2021, 7, 15); //15 July 2021
var today = DateTime.Now;
//get difference of two dates
var diffOfDates = today.Subtract(prevDate);
Console.WriteLine("Difference in Timespan: {0}", diffOfDates);
Console.WriteLine("Difference in Days: {0}", diffOfDates.Days);
Console.WriteLine("Difference in Hours: {0}", diffOfDates.Hours);
Console.WriteLine("Difference in Miniutes: {0}", diffOfDates.Minutes);
Console.WriteLine("Difference in Seconds: {0}", diffOfDates.Seconds);
Console.WriteLine("Difference in Milliseconds: {0}", diffOfDates.Milliseconds);
Console.WriteLine("Difference in Ticks: {0}", diffOfDates.Ticks);
The Subtract()
method subtract TimeSpan
object from the DateTime
object. The TimeSpan
object represents the time in HH:MM:SS
format. The following example demonstrates subtracting TimeSpan
from DateTime
and also DateTime
from DateTime
.
DateTime dt1 = new DateTime(2020, 4, 11, 4, 0, 12); //11 April 2020 4:00:12
DateTime dt2 = new DateTime(2020, 5, 11, 5, 20, 28); //11 May 2020 5:20:28
DateTime dt3 = new DateTime(2020, 6, 11); //11 June 2020 0:00:00
TimeSpan interval = new TimeSpan(2, 14, 18); // 02:14:15
TimeSpan diff1 = dt2.Subtract(dt1); //DateTime - DateTime
TimeSpan diff2 = dt3.Subtract(dt2); //Date - DateTime
DateTime diff3 = dt3.Subtract(interval); //Date - TimeSpan
DateTime diff4 = dt2.Subtract(interval); //DateTime - TimeSpan
//interval.Subtract(dt3); // error
Console.WriteLine("{0} - {1} = {2}", dt2, dt1, diff1);
Console.WriteLine("{0} - {1} = {2}", dt3, dt2, diff2);
Console.WriteLine("{0} - {1} = {2}", dt3, interval, diff3);
Console.WriteLine("{0} - {1} = {2}", dt2, interval, diff4);
05/11/2020 05:20:28 - 04/11/2020 04:00:12 = 30.01:20:16 06/11/2020 00:00:00 - 05/11/2020 05:20:28 = 30.18:39:32 06/11/2020 00:00:00 - 02:14:18 = 06/10/2020 21:45:42 05/11/2020 05:20:28 - 02:14:18 = 05/11/2020 03:06:10