Irakli Lekishvili
Irakli Lekishvili

Reputation: 34158

How get list of datetimes from date range?

I have datetime range:

var _checkInYear = (from d in db.bookings select d.checkinyear).ToList();
var _checkInMonth = (from d in db.bookings select d.checkinmonth).ToList();
var _checkInDay = (from d in db.bookings select d.checkinday).ToList();

var _checkOutYear = (from d in db.bookings select d.checkoutyear).ToList();
var _checkOutMonth = (from d in db.bookings select d.checkoutmonth).ToList();
var _checkOutDay = (from d in db.bookings select d.checkoutday).ToList();

How can I get DateTime list from this range? For example if check in time is 20/08/2011 and check out 23/08/2011 need to put into list date time into this range.

20/08/2011, 21/08/2011, 22/08/2011, 23/08/2011.

Upvotes: 1

Views: 4475

Answers (5)

Yanga
Yanga

Reputation: 3012

A bit old question but i think we should do it lke that:

DateTime checkIn = new DateTime(_checkInYear, _checkInMonth, _checkInDay);
DateTime checkOut = new DateTime(_checkOutYear, _checkOutMonth, _checkOutDay);
List<DateTime> allDates = new List<DateTime> ();

for (DateTime date = checkIn; date <= checkOut; date = date.AddDays(1))
    allDates.Add(date);

Upvotes: 0

TheVillageIdiot
TheVillageIdiot

Reputation: 40497

If you can get check-in and check-out date then you can have a extension method for DateTime to get list:

public static class ExtensionMethods
{
   static IEnumerable<DateTime> GetDateRange(this DateTime d, DateTime e)
   {
        var t=d;
        do
        {
            yield return t;
            t=t.AddDays(1);
        }while(t<e);
    }
}

Then use it like this:

var dateList = checkIn.GetDateRange(checkOutDate);

Tested in Linqpad.

Upvotes: 1

Neil Barnwell
Neil Barnwell

Reputation: 42115

Given that you get your hands on two dates, your best bet is simply to use a for or while loop:

var dates = new List<DateTime>();
var curDate = booking.CheckinDate;
while (curDate <= booking.CheckoutDate)
{
    dates.Add(curDate);
    curDate = curDate.AddDays(1);
}

However, I appreciate this may be a contrived example for the purposes of the question, but I'm concerned your example code won't do what you want. Don't bother reading further if this is the case, I just wanted to highlight it on the off-chance that you might be better off with something like this:

var booking = (from b in data.Bookings
               where b.BookingId = bookingId
               select new BookingSearchResult // You have to create this class
                          {
                              CheckinDate = new DateTime(b.CheckinYear, b.CheckinMonth, b.CheckinDay),
                              CheckoutDate = new DateTime(b.CheckoutYear, b.CheckoutMonth, b.CheckoutDay)
                          }).SingleOrDefault();

Upvotes: 0

Andrew Whitaker
Andrew Whitaker

Reputation: 126042

DateTime checkIn = new DateTime(_checkInYear, _checkInMonth, _checkInDay);
DateTime checkOut = new DateTime(_checkOutYear, _checkOutMonth, _checkOutDay);

TimeSpan span = checkOut - checkIn;
List<DateTime> range = new List<DateTime>();
for(int day = 0; day <= span.Days; day++) 
{
    range.Add(checkIn.AddDays(day));
}

Example: http://www.ideone.com/BxmkF

Upvotes: 3

Anthony Pegram
Anthony Pegram

Reputation: 126834

The algorithm is simple, get your starting point, increment until you reach the ending point.

var startDate = new DateTime(checkInYear, checkInMonth, checkInDay);
var endDate = new DateTime(checkOutYear, checkOutMonth, checkOutDay);
var givenDate = startDate; 
var datesInRange = new List<DateTime>(); 

while (givenDate <= startDate)
{
    datesInRange.Add(givenDate);
    givenDate = givenDate.AddDays(1);
}

// work with / return datesInRange

Upvotes: 2

Related Questions