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
精彩评论