The following code uses TimeSpan and subtract one DateTime from another.
DateTime Date1 = new DateTime(2009, 6, 11);
DateTime Date2 = DateTime.Now;
TimeSpan timeSpan = Date2.Subtract(Date1);
// Display the difference of the two dates up to minutes
Console.WriteLine("Days : " + timeSpan.Days.ToString());
Console.WriteLine("Hours : " + timeSpan.Hours.ToString());
Console.WriteLine("Minutes : " + timeSpan.Minutes.ToString());
// You can also retrieve Total in Hours
Console.WriteLine("Total Hours " + timeSpan.TotalHours.ToString());
For more C# coding tips & tricks, subscribe now.
1 comments:
This code does not take into account changes in daylight savings time. See:
http://awesvb.blogspot.com/2009/04/difference-between-datestimes-including.html
Post a Comment