This code shows you how to find the difference between two DateTime values. The easiest way to do this is to use the TimeSpan class and subtract one DateTime from another.

DateTime firstDate = new DateTime(2008, 8, 1);
DateTime secondDate = DateTime.Now;

TimeSpan timeSpan = secondDate.Subtract(firstDate);

// Get Difference 
Console.WriteLine("Days : " + timeSpan.Days.ToString());
Console.WriteLine("Hours : " + timeSpan.Hours.ToString());
Console.WriteLine("Minutes : " + timeSpan.Minutes.ToString());

// Get Total Hours
Console.WriteLine("Total Hours " + timeSpan.TotalHours.ToString());

Here is the output.

image

Note the difference between Hours and TotalHours.

Tagged with:
 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>