C # calculation of time differencePost By: 2009-2-1 10:51:00
/// <Summary>
/// Overloaded. Calculate the time interval between two dates, and return the absolute value of the date difference of the time interval.
/// </Summary>
/// <Param name = "DateTime1"> first date and time </param>
/// <Param name = "DateTime2"> Second Date and time </param>
/// <Returns> </returns>
Private string DateDiff (DateTime DateTime1, DateTime DateTime2)
{
String dateDiff = null;
Try
{
TimeSpan ts1 = new TimeSpan (DateTime1.Ticks );
TimeSpan ts2 = new TimeSpan (DateTime2.Ticks );
TimeSpan ts = ts1.Subtract (ts2). Duration ();
DateDiff = ts. Days. ToString () + "day"
+ Ts. Hours. ToString () + "hour"
+ Ts. Minutes. ToString () + "Minute"
+ Ts. Seconds. ToString () + "Seconds ";
}
Catch
{
}
Return dateDiff;
}
/// <Summary>
/// Overloaded. Calculate the time interval between a time and the current local Date and time. the return value is the absolute value of the date difference between the time interval.
/// </Summary>
/// <Param name = "DateTime1"> A Date and time </param>
/// <Returns> </returns>
Private string DateDiff (DateTime DateTime1)
{
Return this. DateDiff (DateTime1, DateTime. Now );
}
Note:
1. The DateTime Value Type represents a specific date time between 00:00:00 on January 1, January 1, 0001 AD and on January 1, December 31, 9999 AD. Therefore, you can use the DateTime value type to describe any time within the expected range. A DateTime value represents a specific time.
2. the TimeSpan value contains many attributes and methods for accessing or processing a TimeSpan value.
The following list covers some of them:
Add: adds the value to another TimeSpan value.
Days: returns the TimeSpan value calculated by the number of Days.
Duration: gets the absolute value of TimeSpan.
Hours: returns the hourly TimeSpan value.
Milliseconds: returns the TimeSpan value in Milliseconds.
Minutes: returns the TimeSpan value calculated in Minutes.
Negate: returns the opposite number of the current instance.
Seconds: returns the TimeSpan value calculated in Seconds.
Subtract: Subtract another TimeSpan value from it.
Ticks: the number of tick values returned for TimeSpan.
TotalDays: returns the number of days indicated by the TimeSpan value.
TotalHours: returns the number of hours represented by the TimeSpan value.
TotalMilliseconds: the number of milliseconds in which the TimeSpan value is returned.
TotalMinutes: returns the number of minutes represented by the TimeSpan value.
TotalSeconds: returns the number of seconds represented by the TimeSpan value.
========================
============================ ======================
Label1.Text = "15:36:05 ";
Label2.Text = "20:16:35 ";
DateTime d1 = new DateTime );
DateTime d2 = new DateTime );
TimeSpan d3 = d2.Subtract (d1 );
Label3.Text = "difference :"
+ D3.Days. ToString () + "day"
+ D3.Hours. ToString () + "hour"
+ D3.Minutes. ToString () + "Minute"
+ D3.Seconds. ToString () + "seconds ";
==========================================================================