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.

Download  Run Code

 
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

Download  Run Code

2. Using TimeSpan.TotalHours() method

Download  Run Code

3. Using TimeSpan.TotalMinutes() method

Download  Run Code

4. Using TimeSpan.TotalSeconds() method

Download  Run Code

5. Using TimeSpan.TotalMilliseconds() method

Download  Run Code

 
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.

Download  Run Code

That’s all about calculating the difference between two dates in C#.