开发者

How to count days quantity [duplicate]

This question already has answers here: Calculate difference between two dates (number of days)? (17 answers) 开发者_高级运维 Closed 8 years ago.

I have check in year, month, day and check out year, moth, day. I can not solve problem how i can count how many days in this range


var d1 = new DateTime(year1, month1, day1);
var d2 = new DateTime(year2, month2, day2);
TimeSpan t = d2 - d1;
var elapsedDays = t.Days;


Try this:

TimeSpan difference = endTime.Subtract(startTime); 
int numDays = difference.Days;


Subtracting a DateTime (or a DateTimeOffset) from another will result in a TimeSpan. The TimeSpan structure has a TotalDays property which should give you what you're looking for.

Here's a link to the MSDN page for TimeSpan.


(new DateTime(endYear, endMonth, endDay) - new DateTime(startYear, startMonth, startDay)).TotalDays


DateTime checkin //set to checkin date
DateTime checkout //set to checkout date
TimeSpan ts = checkout.Subtract(checkin);
int dayDifference = ts.TotalDays; //this is your days
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜