tags:

views:

156

answers:

5

Hello. I have two DateTime, and I want to get all DateTimes between these Dates. Such as, if my Dates are like 01.01.2010 - 05.01.2010, my function should return me a list of date (List) and it must contains 01.01.2010, 02.01.2010, 03.01.2010, 04.01.2010, 05.01.2010 I wrote a function like this. It works fine, if my dates in a month. It wont work if my dates be like 01.01.2010 - 05.02.2010. Because the month changed, and my function cant handle it. Is there any function in C# returns me all dates between two date? Or how can I handle month change?

public void GetAllDatesAndInitializeTickets(DateTime startingDate, DateTime endingDate)
    {
        List<DateTime> allDates = new List<DateTime>();

        int starting = startingDate.Day;
        int ending = endingDate.Day;

        for (int i = starting; i <= ending; i++)
        {
            allDates.Add(new DateTime(startingDate.Year, startingDate.Month, i));
        }

EDIT: Question solved, see Tim Robinson's simple answer to use.

+8  A: 

You can use DateTime objects directly in the loop, in place of your int. DateTime.AddDays handles month ends correctly.

for (DateTime date = startingDate; date <= endingDate; date = date.AddDays(1))
    allDates.Add(date);
Tim Robinson
+1 nice solution. Thank you.
Serkan Hekimoglu
+4  A: 
public IEnumerable<DateTime> GetAllDatesAndInitializeTickets(DateTime startingDate, DateTime endingDate)
{
    if (endingDate < startingDate)
    {
        throw new ArgumentException("endingDate should be after startingDate");
    }
    var ts = endingDate - startingDate;
    for (int i = 0; i < ts.TotalDays; i++)
    {
        yield return startingDate.AddDays(i);
    }
}
Darin Dimitrov
Nice use of yield, shoulda thought of that.
Jamiec
A: 

You were so close... just don't use the day, use the whole date.

static IEnumerable<DateTime> GetAllDatesAndInitializeTickets(DateTime startingDate, DateTime endingDate)
{
    List<DateTime> allDates = new List<DateTime>();


    for (DateTime i = startingDate; i <= endingDate; i = i.AddDays(1))
    {
        allDates.Add(i);
    }
    return allDates.AsReadOnly();
}
Jamiec
+2  A: 

How about something like this?

public IEnumerable<DateTime> DateRange(DateTime fromDate, DateTime toDate)
{
    return Enumerable.Range(0, toDate.Subtract(fromDate).Days + 1)
                     .Select(d => fromDate.AddDays(d));
}

Edit: Tested now. :)

Matt Hamilton
A: 

Here is a quick console app to demonstrate how to do it - use AddDays() instead:

class Program
{
    static void Main(string[] args)
    {

        GetDates(new DateTime(2010, 1, 1), new DateTime(2010, 2, 5));

        Console.ReadKey();
    }

    static List<DateTime> GetDates(DateTime startDate, DateTime endDate)
    {
        List<DateTime> dates = new List<DateTime>();

        while ((startDate = startDate.AddDays(1)) < endDate)
        {
            dates.Add(startDate);
        }

        return dates;
    }
}
slugster
Damn, lots of answers to this one.
slugster