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
Asked
Active
Viewed 7,286 times
5 Answers
7
var d1 = new DateTime(year1, month1, day1);
var d2 = new DateTime(year2, month2, day2);
TimeSpan t = d2 - d1;
var elapsedDays = t.Days;
Ivan Danilov
- 13,696
- 5
- 45
- 66
4
Try this:
TimeSpan difference = endTime.Subtract(startTime);
int numDays = difference.Days;
Icemanind
- 45,770
- 48
- 167
- 283
1
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.
Eric Olsson
- 4,707
- 36
- 34
1
(new DateTime(endYear, endMonth, endDay) - new DateTime(startYear, startMonth, startDay)).TotalDays
abatishchev
- 95,331
- 80
- 293
- 426
Kyle W
- 3,650
- 18
- 32
1
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
abatishchev
- 95,331
- 80
- 293
- 426
Tom Studee
- 10,077
- 3
- 36
- 41
-
1.Days will give you 3 in the instance that the total timespan is 34 days. – Kyle W Jul 22 '11 at 21:03