计算两个日期相差几年几个月
?
public static class dateTimeDiff
{
??? /// <summary>
??? /// 計(jì)算日期間隔
??? /// </summary>
??? /// <param name="d1">要參與計(jì)算的其中一個(gè)日期字符串</param>
??? /// <param name="d2">要參與計(jì)算的另一個(gè)日期字符串</param>
??? /// <returns>一個(gè)表示日期間隔的TimeSpan類(lèi)型</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("字符串參數(shù)不正確!");
??????? }
??? }
??? /// <summary>
??? /// 計(jì)算日期間隔
??? /// </summary>
??? /// <param name="d1">要參與計(jì)算的其中一個(gè)日期</param>
??? /// <param name="d2">要參與計(jì)算的另一個(gè)日期</param>
??? /// <returns>一個(gè)表示日期間隔的TimeSpan類(lèi)型</returns>
??? public static TimeSpan toResult(DateTime d1, DateTime d2)
??? {
??????? TimeSpan ts;
??????? if (d1 > d2)
??????? {
??????????? ts = d1 - d2;
??????? }
??????? else
??????? {
??????????? ts = d2 - d1;
??????? }
??????? return ts;
??? }
??? /// <summary>
??? /// 計(jì)算日期間隔
??? /// </summary>
??? /// <param name="d1">要參與計(jì)算的其中一個(gè)日期字符串</param>
??? /// <param name="d2">要參與計(jì)算的另一個(gè)日期字符串</param>
??? /// <param name="drf">決定返回值形式的枚舉</param>
??? /// <returns>一個(gè)代表年月日的int數(shù)組,具體數(shù)組長(zhǎng)度與枚舉參數(shù)drf有關(guān)</returns>
??? public static int[] toResult(string d1, string d2, diffFormat drf)
??? {
??????? try
??????? {
??????????? DateTime date1 = DateTime.Parse(d1);
??????????? DateTime date2 = DateTime.Parse(d2);
??????????? return toResult(date1, date2, drf);
??????? }
??????? catch
??????? {
??????????? throw new Exception("字符串參數(shù)不正確!");
??????? }
??? }
??? /// <summary>
??? /// 計(jì)算日期間隔
??? /// </summary>
??? /// <param name="d1">要參與計(jì)算的其中一個(gè)日期</param>
??? /// <param name="d2">要參與計(jì)算的另一個(gè)日期</param>
??? /// <param name="drf">決定返回值形式的枚舉</param>
??? /// <returns>一個(gè)代表年月日的int數(shù)組,具體數(shù)組長(zhǎng)度與枚舉參數(shù)drf有關(guān)</returns>
??? public static int[] toResult(DateTime d1, DateTime d2, diffFormat drf)
??? {
??????? #region 數(shù)據(jù)初始化
??????? 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 按條件計(jì)算
??????? if (drf == diffFormat.Day)
??????? {
??????????? TimeSpan ts = max - min;
??????????? return new int[] { ts.Days };
??????? }
??????? if (drf == diffFormat.Month)
??????? {
??????????? return new int[] { month + year * 12 };
??????? }
??????? if (drf == diffFormat.Year)
??????? {
??????????? return new int[] { year };
??????? }
??????? return new int[] { year, month };
??????? #endregion
??? }
}
/// <summary>
/// 關(guān)于返回值形式的枚舉
/// </summary>
public enum diffFormat
{
??? /// <summary>
??? /// 年數(shù)和月數(shù)
??? /// </summary>
??? YearMonth,
??? /// <summary>
??? /// 年數(shù)
??? /// </summary>
??? Year,
??? /// <summary>
??? /// 月數(shù)
??? /// </summary>
??? Month,
??? /// <summary>
??? /// 天數(shù)
??? /// </summary>
??? Day,
}
?
例:
??????????????? DateTime dt1 = DateTime.Parse(dt.Rows[0]["mtime"].ToString());
??????????????? DateTime dt2 = DateTime.Parse(dt.Rows[count - 1]["mtime"].ToString());
??????????????? int[] kk = dateTimeDiff.toResult(dt1, dt2, diffFormat.Month);
?
本文轉(zhuǎn)自94cool博客園博客,原文鏈接:http://www.cnblogs.com/94cool/archive/2011/06/13/2079795.html,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的计算两个日期相差几年几个月的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 重建DC的DNS区域
- 下一篇: 动态表单数据库设计