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,469 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
- 14,287
- 6
- 48
- 66
4
Try this:
TimeSpan difference = endTime.Subtract(startTime);
int numDays = difference.Days;

Icemanind
- 47,519
- 50
- 171
- 296
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,805
- 32
- 35
1
(new DateTime(endYear, endMonth, endDay) - new DateTime(startYear, startMonth, startDay)).TotalDays

abatishchev
- 98,240
- 88
- 296
- 433

Kyle W
- 3,702
- 20
- 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
- 98,240
- 88
- 296
- 433

Tom Studee
- 10,316
- 4
- 38
- 42
-
1.Days will give you 3 in the instance that the total timespan is 34 days. – Kyle W Jul 22 '11 at 21:03