How do I get the time difference between two DateTime objects using C#?

C#Datetime

C# Problem Overview


How do I get the time difference between two DateTime objects using C#?

C# Solutions


Solution 1 - C#

The following example demonstrates how to do this:

DateTime a = new DateTime(2010, 05, 12, 13, 15, 00);
DateTime b = new DateTime(2010, 05, 12, 13, 45, 00);
Console.WriteLine(b.Subtract(a).TotalMinutes);

When executed this prints "30" since there is a 30 minute difference between the date/times.

The result of DateTime.Subtract(DateTime x) is a TimeSpan Object which gives other useful properties.

Solution 2 - C#

You want the TimeSpan struct:

TimeSpan diff = dateTime1 - dateTime2;

> A TimeSpan object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. The TimeSpan structure can also be used to represent the time of day, but only if the time is unrelated to a particular date.

There are various methods for getting the days, hours, minutes, seconds and milliseconds back from this structure.

If you are just interested in the difference then:

TimeSpan diff = Math.Abs(dateTime1 - dateTime2);

will give you the positive difference between the times regardless of the order.

If you have just got the time component but the times could be split by midnight then you need to add 24 hours to the span to get the actual difference:

TimeSpan diff = dateTime1 - dateTime2;
if (diff < 0)
{
    diff = diff + TimeSpan.FromDays(1);
}

Solution 3 - C#

What you need is to use the DateTime classs Subtract method, which returns a TimeSpan.

var dateOne = DateTime.Now;
var dateTwo = DateTime.Now.AddMinutes(-5);
var diff = dateTwo.Subtract(dateOne);
var res = String.Format("{0}:{1}:{2}", diff.Hours,diff.Minutes,diff.Seconds));

Solution 4 - C#

The way I usually do it is subtracting the two DateTime and this gets me a TimeSpan that will tell me the diff.

Here's an example:

DateTime start = DateTime.Now;
// Do some work
TimeSpan timeDiff = DateTime.Now - start;
timeDiff.TotalMilliseconds;

Solution 5 - C#

IF they are both UTC date-time values you can do TimeSpan diff = dateTime1 - dateTime2;

Otherwise your chance of getting the correct answer in every single possible case is zero.

Solution 6 - C#

private void button1_Click(object sender, EventArgs e)
{
    TimeSpan timespan;
    timespan = dateTimePicker2.Value - dateTimePicker1.Value;
    int timeDifference = timespan.Days;
    MessageBox.Show(timeDifference.ToString());
}

Solution 7 - C#

You can use in following manner to achieve difference between two Datetime Object. Suppose there are DateTime objects dt1 and dt2 then the code.

TimeSpan diff = dt2.Subtract(dt1);

Solution 8 - C#

 var startDate = new DateTime(2007, 3, 24);
 
 var endDate = new DateTime(2009, 6, 26);

 var dateDiff = endDate.Subtract(startDate);

 var date = string.Format("{0} years {1} months {2} days", (int)dateDiff.TotalDays / 365, 
 (int)(dateDiff.TotalDays % 365) / 30, (int)(dateDiff.TotalDays % 365) / 30);

 Console.WriteLine(date);

Solution 9 - C#

You need to use a TimeSpan. Here is some sample code:

TimeSpan sincelast = TimeSpan.FromTicks(DateTime.Now.Ticks - LastUpdate.Ticks);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAyyappan AnbalaganView Question on Stackoverflow
Solution 1 - C#Daniel RenshawView Answer on Stackoverflow
Solution 2 - C#ChrisFView Answer on Stackoverflow
Solution 3 - C#NateView Answer on Stackoverflow
Solution 4 - C#DarikView Answer on Stackoverflow
Solution 5 - C#Ian MercerView Answer on Stackoverflow
Solution 6 - C#Wong Lok PanView Answer on Stackoverflow
Solution 7 - C#Sunny SandeepView Answer on Stackoverflow
Solution 8 - C#MukundView Answer on Stackoverflow
Solution 9 - C#Jason WebbView Answer on Stackoverflow