日期:2014-05-18  浏览次数:20443 次

日期计算?
怎么可以得到两个日期相差的月份总数,例如:
DateTime firstDate = Convert.ToDateTime("2007-8-1");
DateTime lastDate = Convert.ToDateTime("2008-5-1");
得到相差9个月



------解决方案--------------------
int monthcha=DateDiff("month",firstDate,lastDate);

试试
------解决方案--------------------
Dim a As Date
Dim b As Date
a = System.DateTime.Now
b = CDate(chkExport.Text)
Dim ts As System.TimeSpan
ts = a.Subtract(b)
Dim pp As Integer
pp = CInt(ts.TotalDays)
------解决方案--------------------
DateTime firstDate = Convert.ToDateTime("2007-8-1");
DateTime lastDate = Convert.ToDateTime("2008-5-1");
TimeSpan span = firstDate - lastDate;
WL(Math.Abs(span.Days/30));
------解决方案--------------------
DateTime d1;
DateTime d2;
//赋值
TimeSpan t = d2-d1;

int days = (int)t.TotalDays;
int m = days/30;
------解决方案--------------------
DateTime firstDate = Convert.ToDateTime("2007-8-1");
DateTime lastDate = Convert.ToDateTime("2008-5-1");
TimeSpan diff = lastDate - firstDate;
for (int i = diff.Days/31; i <= diff.Days/28; i++)
{
if (firstDate.AddMonths(i) >= lastDate)
{
// 月数 是 i
break;
}
}