/// <Summary> /// overloaded. returns the absolute value of the date difference between two dates. /// </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 () + "second";} catch {} return datediff;} // <summary> // overloaded. calculates the time interval between a time and the current local date and time, and returns 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 ";