# Date difference calculation (C #)

Source: Internet
Author: User
Tags date1

Datetime time1 = new datetime (198, 24,14 );
Datetime time2 = new datetime (1982,1, 16, 32 );

Timespan Ts = time2.subtract (time1 );
String timespan = "difference :"
+ Ts. Days. tostring () + "day"
+ Ts. Hours. tostring () + "hour"
+ Ts. Minutes. tostring () + "Minute"
+ Ts. Seconds. tostring () + "seconds ";

/// <Summary>
/// Calculate the date interval (static class)
/// </Summary>
Public static class datetimediff
{
/// <Summary>
/// Calculate the date Interval
/// </Summary>
/// <Param name = "d1"> one of the date strings to be calculated </param>
/// <Param name = "D2"> another date string to be calculated </param>
/// <Returns> a timespan type that indicates the date interval </returns>
Public static timespan toresult (string D1, string D2)
{
Try
{
Datetime date1 = datetime. parse (D1 );
Datetime date2 = datetime. parse (D2 );
}
Catch
{
Throw new exception ("the string parameter is incorrect! ");
}
}
/// <Summary>
/// Calculate the date Interval
/// </Summary>
/// <Param name = "d1"> one of the dates to be calculated </param>
/// <Param name = "D2"> another date to be calculated </param>
/// <Returns> a timespan type that indicates the date interval </returns>
Public static timespan toresult (datetime D1, datetime D2)
{
Timespan ts;
If (D1> D2)
{
TS = D1-D2;
}
Else
{
TS = d2-D1;
}
Return ts;
}

/// <Summary>
/// Calculate the date Interval
/// </Summary>
/// <Param name = "d1"> one of the date strings to be calculated </param>
/// <Param name = "D2"> another date string to be calculated </param>
/// <Param name = "DRF"> determines the enumeration in the return value form. </param>
/// <Returns> an int array representing the year, month, and day. The length of the array is related to the enumerated parameter DRF. </returns>
Public static int [] toresult (string D1, string D2, diffresultformat DRF)
{
Try
{
Datetime date1 = datetime. parse (D1 );
Datetime date2 = datetime. parse (D2 );
}
Catch
{
Throw new exception ("the string parameter is incorrect! ");
}
}
/// <Summary>
/// Calculate the date Interval
/// </Summary>
/// <Param name = "d1"> one of the dates to be calculated </param>
/// <Param name = "D2"> another date to be calculated </param>
/// <Param name = "DRF"> determines the enumeration in the return value form. </param>
/// <Returns> an int array representing the year, month, and day. The length of the array is related to the enumerated parameter DRF. </returns>
Public static int [] toresult (datetime D1, datetime D2, diffresultformat DRF)
{
# Region data Initialization
Datetime Max;
Datetime min;
Int year;
Int month;
Int tempyear, 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 + 12;
}
Year = tempyear-min. Year;
Month = tempmonth-min. month;
# Endregion
# Region calculated based on conditions
If (DRF = diffresultformat. dd)
{
Timespan Ts = max-min;
Return new int [] {ts. Days };
}
If (DRF = diffresultformat. mm)
{
Return new int [] {month + year * 12 };
}
If (DRF = diffresultformat. yy)
{
Return new int [] {year };
}
Return new int [] {year, month };
# Endregion
}
}
/// <Summary>
/// Enumeration of returned values
/// </Summary>
Public Enum diffresultformat
{
/// <Summary>
/// Number of years and months
/// </Summary>
Yymm,
/// <Summary>
/// Number of years
/// </Summary>
YY,
/// <Summary>
/// Number of months
/// </Summary>
Mm,
/// <Summary>
/// Number of days
/// </Summary>
DD,
}

Next we will use this class to calculate the date interval:

String str1 = "2007-12-31 ";
String str2 = "2009-6-1 ";

Int [] KK = datetimediff. toresult (str1, str2, diffresultformat. mm );
Console. writeline (string. Format ("interval: {0} months", kk );
// The result is displayed as follows: interval: 18 months

Datetime 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} month", kk2 , kk2 );
// The result is displayed as follows: interval: 1 year 6 months

You can also use this class to calculate the time interval:

String str3 = "1:55:24 ";
String str4 = "2009-6-1 ";

int kk3 = datetimediff. toresult (str3, str4 ). hours;
console. writeline (string. format ("interval: {0} hours", kk3);
// The result is displayed as follows: interval: 22 hours

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.