Reprint: http://www.cnblogs.com/wifi/articles/2439916.html
You can add two dates between any one unit of time.
private String DateDiff (DateTime DateTime1, DateTime DateTime2) {string DateDiff = null; TimeSpan ts = datetime1.subtract (DateTime2). Duration (); DateDiff = ts. Days.tostring () + "Day" + TS. Hours.tostring () + "hours" + TS. Minutes.tostring () + "minutes" + TS. Seconds.tostring () + "seconds"; return DateDiff; }
Description
The 1.DateTime value type represents a specific date time from January 1, 01 0:0 0 seconds to the December 31, 9999 23:59 59 seconds. Therefore, you can use a DateTime value type to describe any time that is within the scope of your imagination. A datetime value represents a specific moment.
The 2.TimeSpan value contains many properties and methods for accessing or processing a TimeSpan value
The following list covers a subset of these:
Add: Adds to another timespan value.
Days: Returns the TimeSpan value that is calculated in day.
Duration: Gets the absolute value of the timespan.
Hours: Returns the TimeSpan value computed in hours
Milliseconds: Returns the TimeSpan value computed in milliseconds.
Minutes: Returns the TimeSpan value computed in minutes.
Negate: Returns the inverse number of the current instance.
Seconds: Returns the TimeSpan value computed in seconds.
Subtract: Subtracts another TimeSpan value from it.
Ticks: Returns the number of ticks for the timespan value.
Totaldays: Returns the number of days that the TimeSpan value represents.
TotalHours: Returns the number of hours that the timespan value represents.
TotalMilliseconds: Returns the number of milliseconds represented by a timespan value.
Totalminutes: Returns the number of minutes represented by a timespan value.
TotalSeconds: Returns the number of seconds represented by a timespan value.
C # uses a TimeSpan to calculate two time difference