views:

582

answers:

4

Hello! I have a calendar which passes selected dates as strings into a method. Inside this method, I want to generate a list of all the dates starting from the selected start date and ending with the selected end date, obviously including all of the dates inbetween, regardless of how many days are inbetween the selected start and end dates.

Below I have the beginning of the method which takes the date strings and converts them into DateTime variables so that I can make use of the DateTime calculation functions. However, I cannot seem to work out how to calculate all of the dates inbetween the start and end date? Obviously the first stage is to subtract the start date from the end date, but I cannot calculate the rest of the steps.

Help appreciated greatly,

kind regards.

public void DTCalculations()
{
List<string> calculatedDates = new List<string>();
string startDate = "2009-07-27";
string endDate = "2009-07-29";

//Convert to DateTime variables
DateTime start = DateTime.Parse(startDate);
DateTime end = DateTime.Parse(endDate);

//Calculate difference between start and end date.
TimeSpan difference =  end.Subtract(start);

//Generate list of dates beginning at start date and ending at end date.
//ToDo:
}
+2  A: 

The easiest thing to do would be take the start date, and add 1 day to it (using AddDays) until you reach the end date. Something like this:

DateTime calcDate = start.Date;
while (calcDate <= end)
{
    calcDate = calcDate.AddDays(1);
    calculatedDates.Add(calcDate.ToString());
}

Obviously, you would adjust the while conditional and the position of the AddDays call depending on if you wanted to include the start and end dates in the collection or not.

[Edit: By the way, you should consider using TryParse() instead of Parse() in case the passed in strings don't convert to dates nicely]

Simon P Stevens
Make sure that the end date is greater than the start date too, so you won't get into an infinite loop. :)
Greg Hewgill
Greg, you shouldn't check with == but with <=
Henk Holterman
A: 
for( DateTime i = start; i <= end; i = i.AddDays( 1 ) )
{
    Console.WriteLine(i.ToShortDateString());
}
Timbo
+3  A: 

You just need to iterate from start to end, you can do this in a for loop

DateTime start = DateTime.Parse(startDate);
DateTime end = DateTime.Parse(endDate);

for(DateTime counter = start; counter <= end; counter = counter.AddDays(1))
{
    calculatedDates.Add(counter);
}
Binary Worrier
+10  A: 
static IEnumerable<DateTime> AllDatesBetween(DateTime start, DateTime end)
{
    for(var day = start.Date; day <= end; day = day.AddDays(1))
        yield return day;
}

Edit: Added code to solve your particular example and to demonstrate usage:

var calculatedDates = 
    new List<string>
    (
        AllDatesBetween
        (
            DateTime.Parse("2009-07-27"),
            DateTime.Parse("2009-07-29")
        ).Select(d => d.ToString("yyyy-MM-dd"))
    );
Matt Howells
I always feel the yield keyword is far too under used in most peoples .net +1 for spot on usage!
MPritch
For completeness, I'd make it static too
MPritch