/// <summary>
/// Calculate the interval of dates (static class)
/// </summary>
public static class dateTimeDiff
{
#region Calculate the date interval
/// <summary>
/// Calculate the daily 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 day interval</returns>
public static TimeSpan toResult(string d1, string d2)
{
try
{
DateTime date1 = (d1);
DateTime date2 = (d2);
return toResult(date1, date2);
}
catch
{
throw new Exception("String parameter is incorrect!");
}
}
#endregion
#region Calculate the date interval
/// <summary>
/// Calculate the daily 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 day interval</returns>
public static TimeSpan toResult(DateTime d1, DateTime d2)
{
TimeSpan ts;
if (d1 > d2)
{
ts = d1 - d2;
}
else
{
ts = d2 - d1;
}
return ts;
}
#endregion
#region Calculate the date interval
/// <summary>
/// Calculate the daily 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">Enum of return value</param>
/// <returns> An int array representing year, month and day. The specific array length is related to the enumeration parameter drf</returns>
public static int[] toResult(string d1, string d2, diffResultFormat drf)
{
try
{
DateTime date1 = (d1);
DateTime date2 = (d2);
return toResult(date1, date2, drf);
}
catch
{
throw new Exception("String parameter is incorrect!");
}
}
#endregion
#region Calculate the date interval
/// <summary>
/// Calculate the daily 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">Enum of return value</param>
/// <returns> An int array representing year, month and day. The specific array length is related to the enumeration 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 = ;
tempMonth = ;
if ( < )
{
tempYear--;
tempMonth = tempMonth + 12;
}
year = tempYear - ;
month = tempMonth - ;
#endregion
#region Calculate by condition
if (drf == )
{
TimeSpan ts = max - min;
return new int[] { };
}
if (drf == )
{
return new int[] { month + year * 12 };
}
if (drf == )
{
return new int[] { year };
}
return new int[] { year, month };
#endregion
}
#endregion
}
#region About the enumeration of return value form
/// <summary>
/// About the enumeration of return value form
/// </summary>
public enum diffResultFormat
{
/// <summary>
/// Years and months
/// </summary>
yymm,
/// <summary>
/// Years
/// </summary>
yy,
/// <summary>
/// Months
/// </summary>
mm,
/// <summary>
/// Number of days
/// </summary>
dd,
}
#endregion