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;
* 核算:开始日期-结束日期=月份数
还没有评论,来说两句吧...