public int GetMonths(DateTime from, DateTime to)
{
int years = to.Year - from.Year;
int months = to.Month - from.Month;
int days = to.Day - from.Day;
if (days < 0)
months = months - 1;
return years * 12 + months;
}
此方法只精确到日期,如果要精确到时分秒,以此类推,只要后面的单位相减为负数,则上一个单位就-1.