日期差計算(C#)

來源:互聯網
上載者:User

DateTime time1 =new DateTime(1982,4,24,14,23,06);
DateTime time2 =new DateTime(1982,1,21,8,16,32);

TimeSpan ts= time2.Subtract(time1);
string timespan = "相差:"
+ts.Days.ToString()+"天"
+ts.Hours.ToString()+"小時"
+ts.Minutes.ToString()+"分鐘"
+ts.Seconds.ToString()+"秒";

 

 

 

/// <summary>
/// 計算日期的間隔(靜態類)
/// </summary>
public static class dateTimeDiff
{
    /// <summary>
    /// 計算日期間隔
    /// </summary>
    /// <param name="d1">要參與計算的其中一個日期文字</param>
    /// <param name="d2">要參與計算的另一個日期文字</param>
    /// <returns>一個表示日期間隔的TimeSpan類型</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("字串參數不正確!");
        }
    }
    /// <summary>
    /// 計算日期間隔
    /// </summary>
    /// <param name="d1">要參與計算的其中一個日期</param>
    /// <param name="d2">要參與計算的另一個日期</param>
    /// <returns>一個表示日期間隔的TimeSpan類型</returns>
    public static TimeSpan toResult(DateTime d1, DateTime d2)
    {
        TimeSpan ts;
        if (d1 > d2)
        {
            ts = d1 - d2;
        }
        else
        {
            ts = d2 - d1;
        }
        return ts;
    }

    /// <summary>
    /// 計算日期間隔
    /// </summary>
    /// <param name="d1">要參與計算的其中一個日期文字</param>
    /// <param name="d2">要參與計算的另一個日期文字</param>
    /// <param name="drf">決定傳回值形式的枚舉</param>
    /// <returns>一個代表年月日的int數組,具體數組長度與枚舉參數drf有關</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("字串參數不正確!");
        }
    }
    /// <summary>
    /// 計算日期間隔
    /// </summary>
    /// <param name="d1">要參與計算的其中一個日期</param>
    /// <param name="d2">要參與計算的另一個日期</param>
    /// <param name="drf">決定傳回值形式的枚舉</param>
    /// <returns>一個代表年月日的int數組,具體數組長度與枚舉參數drf有關</returns>
    public static int[] toResult(DateTime d1, DateTime d2, diffResultFormat drf)
    {
        #region 資料初始化
        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 按條件計算
        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>
/// 關於傳回值形式的枚舉
/// </summary>
public enum diffResultFormat
{
    /// <summary>
    /// 年數和月數
    /// </summary>
    yymm,
    /// <summary>
    /// 年數
    /// </summary>
    yy,
    /// <summary>
    /// 月數
    /// </summary>
    mm,
    /// <summary>
    /// 天數
    /// </summary>
    dd,
}

 

下面我們將使用這個類來計算日期間隔:

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

        int[] kk = dateTimeDiff.toResult(str1, str2, diffResultFormat.mm);
        Console.WriteLine(string.Format("間隔:{0}個月", kk[0]));
        //結果顯示為:間隔:18個月

        DateTime date1 = DateTime.Parse(str1);
        DateTime date2 = DateTime.Parse(str2);

        int[] kk2 = dateTimeDiff.toResult(date1, date2, diffResultFormat.yymm);
        Console.WriteLine(string.Format("間隔:{0}年{1}個月", kk2[0], kk2[1]));
        //結果顯示為:間隔:1年6個月

也可以用這個類來計算時間間隔:

        string str3 = "2009-5-31 1:55:24";
        string str4 = "2009-6-1";

        int kk3 =dateTimeDiff.toResult(str3, str4).Hours;
        Console.WriteLine(string.Format("間隔:{0}個小時", kk3));
        //結果顯示為:間隔:22個小時

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.