Tuesday, March 8, 2011

TimeSpan , DateDiff , and millisecond date formatting in .net

CODE :


// Start dateDateTime startDate = new DateTime(2005, 2, 1, 3, 4, 12, 56);
// End dateDateTime endDate = new DateTime(2005, 12, 12, 4, 30, 45, 12);
// Time span TimeSpan diffDate = endDate.Subtract(startDate);
// Spit it out Label1.Text = "Start Date:" + startDate.ToString("yyyy-MM-dd HH:mm:ss.fff") + "<br>" + "End Date:" + endDate.ToString("yyyy-MM-dd HH:mm:ss.fff") +
"<br>" + "Time Difference: " ;
Label1.Text += (diffDate.Days.ToString() + " Days,");
Label1.Text += (diffDate.Hours.ToString() + " Hours,");
Label1.Text += (diffDate.Minutes.ToString() + " Minutes,");
Label1.Text += (diffDate.Seconds.ToString() + " Seconds,");
Label1.Text += (diffDate.Milliseconds.ToString() + " Milliseconds " + "<br>");
Label1.Text += "Total TimeDifference: " + diffDate.TotalMinutes + "Minutes = " + diffDate.TotalHours + "Hours = ";
Label1.Text += diffDate.TotalMilliseconds + "Millisecs = " + diffDate.TotalDays + "Days";

OUTPUT :

Start Date:2005-02-01 03:04:12.056
End Date:2005-12-12 04:30:45.012
Time Difference: 314 Days,1 Hours,26 Minutes,32 Seconds,956 Milliseconds
Total TimeDifference: 452246.549266667Minutes = 7537.44248777778Hours = 27134792956Millisecs = 314.060103657407Days


No comments:

Post a Comment