码迷,mamicode.com
首页 > 其他好文 > 详细

计算日期间相差月数

时间:2015-10-14 09:06:41      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:


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.

计算日期间相差月数

标签:

原文地址:http://www.cnblogs.com/tylertang/p/4876394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!