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

如何计算两个DateTime类型之间的差值.要求返回天数..
如题目..
int   days;
DateTime   dt1,dt2;

days=dt1-dt2..

谢谢

------解决方案--------------------
Timespan a = dt1 - dt2;
a.totaldays还是啥的
------解决方案--------------------
TimeSpan sub = dt1.Substract(dt2);
Console.WriteLine( "substract : {0} ", sub.Days.ToString());
------解决方案--------------------
double tianshu;
DateTime t1;
DateTime t2;



t1= Convert.ToDateTime( "2007-09-09 12:00:00 ");

t2= Convert.ToDateTime( "2007-09-19 16:00:00 ");

TimeSpan diff = t1-t2;


//tianshu 就是相差的天数,不过带小数部分,不需要就自己去掉
tianshu = diff.TotalDays;

MessageBox.Show(tianshu.ToString());
------解决方案--------------------
Timespan