Example of c date interval calculation

  • 2020-06-12 10:32:22
  • OfStack


/// <summary>
///  Calculate the interval between dates ( Static class )
/// </summary>
public static class dateTimeDiff
{
#region  Calculate date interval 
/// <summary>
///  Calculate date interval 
/// </summary>
/// <param name="d1"> It's part of the calculation 1 Date string </param>
/// <param name="d2"> To participate in the calculation of the other 1 Date string </param>
/// <returns>1 That represents a date interval TimeSpan type </returns>
public static TimeSpan toResult(string d1, string d2)
{
try
{
DateTime date1 = DateTime.Parse(d1);
DateTime date2 = DateTime.Parse(d2);
return toResult(date1, date2);
}
catch
{
throw new Exception(" The string parameter is incorrect !");
}
} 
#endregion
#region  Calculate date interval 
/// <summary>
///  Calculate date interval 
/// </summary>
/// <param name="d1"> It's part of the calculation 1 A date </param>
/// <param name="d2"> To participate in the calculation of the other 1 A date </param>
/// <returns>1 That represents a date interval TimeSpan type </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 date interval 
/// <summary>
///  Calculate date interval 
/// </summary>
/// <param name="d1"> It's part of the calculation 1 Date string </param>
/// <param name="d2"> To participate in the calculation of the other 1 Date string </param>
/// <param name="drf"> An enumeration that determines the form of the return value </param>
/// <returns>1 Of the year, month and day int Array, specific array length with enumeration parameters drf The relevant </returns>
public static int[] toResult(string d1, string d2, diffResultFormat drf)
{
try
{
DateTime date1 = DateTime.Parse(d1);
DateTime date2 = DateTime.Parse(d2);
return toResult(date1, date2, drf);
}
catch
{
throw new Exception(" The string parameter is incorrect !");
}
} 
#endregion
#region  Calculate date interval 
/// <summary>
///  Calculate date interval 
/// </summary>
/// <param name="d1"> It's part of the calculation 1 A date </param>
/// <param name="d2"> To participate in the calculation of the other 1 A date </param>
/// <param name="drf"> An enumeration that determines the form of the return value </param>
/// <returns>1 Of the year, month and day int Array, specific array length with enumeration parameters drf The relevant </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  On condition 
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
} 
#endregion
}
#region  Enumeration of return value forms 
/// <summary>
///  Enumeration of return value forms 
/// </summary>
public enum diffResultFormat
{
/// <summary>
///  Years and months 
/// </summary>
yymm,
/// <summary>
///  Number of years 
/// </summary>
yy,
/// <summary>
///  months 
/// </summary>
mm,
/// <summary>
///  Number of days 
/// </summary>
dd,
} 
#endregion


DateTime sDate = Convert.ToDateTime("2014-1-16");
DateTime eDate = Convert.ToDateTime("2014-2-16");
int month = dateTimeDiff.toResult(sDate, eDate, diffResultFormat.mm)[0];


Related articles: