C method code sharing for calculating two time differences
- 2020-05-26 10:03:14
- OfStack
1. First, introduce a few simple concepts. As follows:
1.DateTime
The DateTime value type represents a specific date between 0:0:0sec, January 1, 0001, and 239:59s, December 31, 9999.
Therefore, you can use the DateTime value type to describe any time in the imaginary range. An DateTime value represents a specific moment
2.TimeSpan
The TimeSpan value contains a number of properties and methods for accessing or processing an TimeSpan value
The following list covers part 1:
Add: add to another TimeSpan value.
Days: returns the TimeSpan value in days.
Duration: gets the absolute value of TimeSpan.
Hours: returns the value of TimeSpan in hours
Milliseconds: returns the value of TimeSpan in milliseconds.
Minutes: returns the value of TimeSpan calculated in minutes.
Negate: returns the negative of the current instance.
Seconds: returns the value of TimeSpan in seconds.
Subtract: subtract another TimeSpan value from it.
Ticks: returns the number of tick for the TimeSpan value.
TotalDays: returns the number of days represented by the TimeSpan value.
TotalHours: returns the number of hours represented by the TimeSpan value.
TotalMilliseconds: returns the number of milliseconds represented by the TimeSpan value.
TotalMinutes: returns the number of minutes represented by the TimeSpan value.
TotalSeconds: returns the number of seconds represented by the TimeSpan value.
2. According to the above introduction, give an example.
/// <summary>
/// Calculated time difference
/// </summary>
/// <param name="t"> time 1</param>
/// <param name="t2"> time 2</param>
/// <returns> Return value: time difference ( milliseconds )</returns>
private long TimeDiff(DateTime t, DateTime t2)
{
long lReturn = -1;
System.TimeSpan NowValue = new TimeSpan(t.Ticks);
System.TimeSpan TimeValue = new TimeSpan(t2.Ticks);
System.TimeSpan DateDiff = TimeSpan.Zero;
try
{
// Calculated time difference
//DateDiff = TimeValue.Subtract(NowValue).Duration();
DateDiff = TimeValue.Subtract(NowValue);
int hours = DateDiff.Hours;
int minutes = DateDiff.Minutes;
int seconds = DateDiff.Seconds;
int milliseconds = DateDiff.Milliseconds;
string TimeDiff = hours.ToString() + ":"
+ minutes.ToString() + ":"
+ seconds.ToString() + "."
+ milliseconds.ToString();
Program.log.WriteLog(" Time: "+TimeDiff, System.DateTime.Now.ToString(), Orid.Log.LogManagerBase.LogMode.logNormal);
// Whether it is smaller than the current time, if it is smaller, set it to no 2 Start on the next day, or start on the same day
if (hours <= 0 && minutes <= 0 && seconds <= 0 && milliseconds <= 0)
hours += 24;
lReturn = hours * 3600 * 1000
+ minutes * 60 * 1000
+ seconds * 1000
+ milliseconds;
}
catch (Exception e) {
throw new Exception(e.Message);
}
return lReturn;
}
3. Call this function at last.
long dueTime = this.TimeDiff(System.DateTime.Now, Convert.ToDateTime(config.Pitch));
//timer = new System.Threading.Timer(tcb, auto, dueTime, System.Threading.Timeout.Infinite);
timer = new System.Threading.Timer(tcb, auto, dueTime, 24 * 3600 * 1000);
To complete.