Calculate difference between two dates (number of days)?

I see that this question has been answered for Java, JavaScript, and PHP, but not C#. So, how might one calculate the number of days between two dates in C#?


假设StartDateEndDate的类型为DateTime

(EndDate - StartDate).TotalDays

使用日期减法结果的TimeSpan对象:

DateTime d1;
DateTime d2;
return (d1 - d2).TotalDays;

The top answer is correct, however if you would like only WHOLE days as an int and are happy to forgo the time component of the date then consider:

(EndDate.Date - StartDate.Date).Days

Again assuming StartDate and EndDate are of type DateTime.

链接地址: http://www.djcxy.com/p/6242.html

上一篇: 获取Android上的当前时间和日期

下一篇: 计算两个日期之间的差异(天数)?