The following sample code shows how to calculate the difference between two DateTime objects using C# syntax.
DateTime dtDate1= new DateTime(2007, 3,7, 12, 0, 0); DateTime dtDate2 = new DateTime(2007, 3, 12, 0, 0, 0);
TimeSpan dtDiff = dtDate2 -dtDate1;
MessageBox.Show(dtDiff.ToString());
// If you want the result in No of days MessageBox.Show(dtDiff.Days.ToString());
// Total Minutes in the TimeSpan string minutes = dtDiff.TotalMinutes();
// Total Hours in the TimeSpan string minutes = dtDiff.TotalHours();
|
| Author: Kapil Dhawan 17 Jun 2008 | Member Level: Gold Points : 2 |
Hello Nice piece of code Thanks for sharing your knowledge with us. I hope to see more good code from your side This code will help lots of guys Thanks to you Regards, Kapil
|