/// <summary>///calculate the interval of a date (static Class)/// </summary> Public Static classdatetimediff{/// <summary> ///Calculate date interval/// </summary> /// <param name= "D1" >One of the date strings to participate in the calculation</param> /// <param name= "D2" >Another date string to participate in the calculation</param> /// <returns>a TimeSpan type that represents the date interval</returns> Public StaticTimeSpan Toresult (stringD1,stringD2) { Try{DateTime date1=DateTime.Parse (D1); DateTime Date2=DateTime.Parse (D2); returnToresult (date1, date2); } Catch { Throw NewException ("string parameter is incorrect!"); } } /// <summary> ///Calculate date interval/// </summary> /// <param name= "D1" >One of the dates to participate in the calculation</param> /// <param name= "D2" >another date to participate in the calculation</param> /// <returns>a TimeSpan type that represents the date interval</returns> Public StaticTimeSpan Toresult (datetime d1, datetime d2) {TimeSpan TS; if(D1 >D2) {TS= d1-D2; } Else{TS= D2-D1; } returnts; } /// <summary> ///Calculate date interval/// </summary> /// <param name= "D1" >One of the date strings to participate in the calculation</param> /// <param name= "D2" >Another date string to participate in the calculation</param> /// <param name= "DRF" >enumeration that determines the form of a return value</param> /// <returns>An int array representing the month and day, with the specific array length associated with the enumeration parameter DRF</returns> Public Static int[] Toresult (stringD1,stringD2, Diffresultformat DRF) { Try{DateTime date1=DateTime.Parse (D1); DateTime Date2=DateTime.Parse (D2); returnToresult (date1, Date2, DRF); } Catch { Throw NewException ("string parameter is incorrect!"); } } /// <summary> ///Calculate date interval/// </summary> /// <param name= "D1" >One of the dates to participate in the calculation</param> /// <param name= "D2" >another date to participate in the calculation</param> /// <param name= "DRF" >enumeration that determines the form of a return value</param> /// <returns>An int array representing the month and day, with the specific array length associated with the enumeration parameter DRF</returns> Public Static int[] Toresult (datetime d1, datetime d2, Diffresultformat DRF) {#regionData initializationDateTime Max; DateTime min; intYear ; intmonth; intTempyear, Tempmonth; if(D1 >D2) {Max=D1; Min=D2; } Else{Max=D2; Min=D1; } tempyear=Max. year; Tempmonth=Max. Month; if(max. Month <min. Month) {tempyear--; Tempmonth= Tempmonth + A; } Year= Tempyear-min. year; Month= Tempmonth-min. Month; #endregion #regionCalculation by conditionif(DRF = =diffresultformat.dd) {TimeSpan TS= max-min; return New int[] {ts. Days}; } if(DRF = =diffresultformat.mm) {return New int[] {month + year * A }; } if(DRF = =diffresultformat.yy) {return New int[] {year}; } return New int[] {year, month}; #endregion }}/// <summary>///enumeration about the form of return values/// </summary> Public enumdiffresultformat{/// <summary> ///number of years and months/// </summary>Yymm,/// <summary> ///Number of years/// </summary>yy,/// <summary> ///number of months/// </summary>mm,/// <summary> ///days/// </summary>DD,} Below we will use this class to calculate the date interval:stringSTR1 ="2007-12-31"; stringSTR2 ="2009-6-1"; int[] KK =Datetimediff.toresult (str1, str2, diffresultformat.mm); Console.WriteLine (string. Format ("interval: {0} months", kk[0])); //the results are shown as: Interval: 18 monthsDateTime date1=DateTime.Parse (STR1); DateTime Date2=DateTime.Parse (STR2); int[] Kk2 =Datetimediff.toresult (date1, Date2, diffresultformat.yymm); Console.WriteLine (string. Format ("interval: {0} year {1} months", kk2[0], kk2[1])); //the results are shown as: Interval: 1 years 6 monthsYou can also use this class to calculate the time interval:stringSTR3 ="2009-5-31 1:55:24"; stringSTR4 ="2009-6-1"; intKK3 =Datetimediff.toresult (STR3, STR4). Hours; Console.WriteLine (string. Format ("interval: {0} hours", KK3)); //the results are shown as: Interval: 22 hours
C # Date Calculation