C# 兩個日期相減覈算月份

 #region 兩個日期相減得月份
    public string GetMonth(string startTime, string endTime)
    {
        string msg = string.Empty;
        DateTime startDate = DateTime.Parse(startTime);
        DateTime endDate = DateTime.Parse(endTime);   //結束時間-起始時間
        int totalMonth = endDate.Year * 12 + endDate.Month - startDate.Year * 12 - startDate.Month;
        if (totalMonth < 3)
        {
            msg = "3個月以下";
        }
        else if (totalMonth >= 3 & totalMonth < 12)
        {
            msg = "3月-1年";
        }
        else if (totalMonth >= 12 & totalMonth < 24)
        {
            msg = "1年-2年";
        }
        else if (totalMonth >= 24 & totalMonth < 36)
        {
            msg = "2年-3年";
        }
        else if (totalMonth >= 36 & totalMonth < 48)
        {
            msg = "3年-4年";
        }
        else if (totalMonth >= 48)
        {
            msg = "5年以上";
        }
        return msg;
    }
    #endregion 

 

 int totalMonth = endDate.Year * 12 + endDate.Month - startDate.Year * 12 - startDate.Month;

* 覈算:開始日期-結束日期=月份數

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章