#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;
* 覈算:開始日期-結束日期=月份數