The method of calculating the time interval c #

The method of calculating the time interval c #

One, two DateTime calculation interval, there are two methods :

1, two DateTime directly subtracted, to obtain a TimeSpan, showing two time intervals

2, carried out with DateTime.Subtract (DateTime) method of calculation, the results with the same method, substituting the parameters expressed by subtracting the time with the current time, a return TimeSpan 

 

Two, the TimeSpan structure represents a time interval, by the number of positive and negative days, hours, minutes, seconds and fractions of seconds to measure.

TimeSpan text object (.tostring ()) "11.13: 46: 40", denotes 11 days, 13 hours, 46 minutes and 40 seconds.

TimeSpan.TotalSeconds property: Get the current value of the fractional part TimeSpan structure to whole seconds and the second representation, it may be a negative number. Coming days, hours, minutes, seconds into seconds display.

TimeSpan of Days, Hours, minutes, seconds properties: each time interval for acquiring days, hours, minutes, seconds. 

 

Here is an example: wherein the same value ts, and ts2.

DateTime dTimeEnd = examAirTime.AddMinutes(examPeriod);

DateTime dTimeServer = dataClass.getDateServer();

TimeSpan ts = dTimeEnd - dTimeServer;

TimeSpan ts2 = dTimeEnd.Subtract(dTimeServer);

MessageBox.Show(ts.ToString() + "\n" + ts2.TotalMinutes.ToString());

Released three original articles · won praise 2 · Views 2825

Guess you like

Origin blog.csdn.net/fangyuan621/article/details/105316940