First, let's look at the simplest. net code for calculating the time difference.
| The code is as follows: |
Copy code |
|
DateTime dt1 = new DateTime );
DateTime dt2 = new DateTime );
System. TimeSpan st = dt2.sSubtract (dt1 );
This. TextBox1.Text = st. Days. ToString () + "day"
+ St. Hours. ToString () + "hour"
+ St. Minutes. ToString () + "minute"
+ St. Seconds. ToString () + "Seconds "; |
Instances, such as the number of days and seconds before the date and time
Function:
| The code is as follows: |
Copy code |
/// <Summary>
/// Return the time interval between two dates (y: year interval, M: month interval, d: day interval, h: hour interval, m: minute interval, s: second interval)
/// </Summary>
/// <Param name = "Date1"> start date </param>
/// <Param name = "Date2"> end date </param>
/// <Param name = "Interval"> Interval flag </param>
/// <Returns> return interval indicates the specified interval </returns>
Private int DateDiff (System. DateTime Date1, System. DateTime Date2, string Interval)
{
Double dblYearLen = 365; // The length of the year, 365 days
Double dblMonthLen = (365/12); // average number of days per month
System. TimeSpan objT;
ObjT = Date2.Subtract (Date1 );
Switch (Interval)
{
Case "y": // returns the year interval of the date.
Return System. Convert. ToInt32 (objT. Days/dblYearLen );
Case "M": // return the month interval of the date
Return System. Convert. ToInt32 (objT. Days/dblMonthLen );
Case "d": // The number of days before the return date.
Return objT. Days;
Case "h": // returns the hour interval of the date
Return System. Convert. ToInt32 (objT. TotalHours );
Case "m": // return the minute interval of the date
Return System. Convert. ToInt32 (objT. TotalMinutes );
Case "s": // return the second interval of the date
Return System. Convert. ToInt32 (objT. TotalSeconds );
Default:
Break;
}
Return 0;
} |
Usage:
| The code is as follows: |
Copy code |
|
Private void Form1_Load (object sender, System. EventArgs e)
{
System. DateTime dt1, dt2;
Dt1 = new DateTime (November 5, 1 );
Dt2 = new DateTime (November 6, 30 );
System. Console. WriteLine (this. DateDiff (dt1, dt2, "Y "));
System. Console. WriteLine (this. DateDiff (dt1, dt2, "M "));
System. Console. WriteLine (this. DateDiff (dt1, dt2, "d "));
System. Console. WriteLine (this. DateDiff (dt1, dt2, "h "));
System. Console. WriteLine (this. DateDiff (dt1, dt2, "m "));
System. Console. WriteLine (this. DateDiff (dt1, dt2, "s "));
} |
Instance
| The code is as follows: |
Copy code |
|
DateTime dt1, dt2; // if not the time type, convert it to the time type
TimeSpan d = dt1.Subtract (dt2 );
D. Days =... // This is the number of Days
D. Minutes =... // This is minute
Public enum DateInterval
{
Second, Minute, Hour, Day, Week, Month, Quarter, Year
}
Public sealed class DateTimeManger
{
Private DateTimeManger ()
{}// End of default constructor
Public static long DateDiff (DateInterval Interval, System. DateTime StartDate, System. DateTime EndDate)
{
Long lngDateDiffValue = 0;
System. TimeSpan TS = new System. TimeSpan (EndDate. Ticks-StartDate. Ticks );
Switch (Interval)
{
Case DateInterval. Second:
LngDateDiffValue = (long) TS. TotalSeconds;
Break;
Case DateInterval. Minute:
LngDateDiffValue = (long) TS. TotalMinutes;
Break;
Case DateInterval. Hour:
LngDateDiffValue = (long) TS. TotalHours;
Break;
Case DateInterval. Day:
LngDateDiffValue = (long) TS. Days;
Break;
Case DateInterval. Week:
LngDateDiffValue = (long) (TS. Days/7 );
Break;
Case DateInterval. Month:
LngDateDiffValue = (long) (TS. Days/30 );
Break;
Case DateInterval. Quarter:
LngDateDiffValue = (long) (TS. Days/30)/3 );
Break;
Case DateInterval. Year:
LngDateDiffValue = (long) (TS. Days/365 );
Break;
}
Return (lngDateDiffValue );
} // End of DateDiff
} // End of class |
The call method is simple in the form of a function.