Create an array or List of all dates between two dates

I am generating multi-series graphs with the date along the X-Axis.

The problem is that not all of the series in the graph have the same dates in the date range. Meaning that if I choose 1 Feb through 30 Apr that one series may have data that starts at 1 Feb but only goes through the end of March but another series may have data for the entire date range.

This skews the charts I need to create. Go, given the date range taken at the begining of the query I’d like to generate a list of dates and populate the data to be graphed, padding those series with 0’s for those dates that have no data.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1


Enumerable.Range(0, 1 + end.Subtract(start).Days)
          .Select(offset => start.AddDays(offset))

For loop:

var dates = new List<DateTime>();

for (var dt = start; dt <= end; dt = dt.AddDays(1))

As for padding values with defaults in a time-series, you could enumerate all the dates in the full date-range, and pick the value for a date directly from the series if it exists, or the default otherwise. For example:

var paddedSeries = fullDates.ToDictionary(date => date, date => timeSeries.ContainsDate(date) 
                                               ? timeSeries[date] : defaultValue);

Method 2

public static IEnumerable<DateTime> GetDateRange(DateTime startDate, DateTime endDate)
    if (endDate < startDate)
        throw new ArgumentException("endDate must be greater than or equal to startDate");

    while (startDate <= endDate)
        yield return startDate;
        startDate = startDate.AddDays(1);

Method 3

I know this is an old post but try using an extension method:

    public static IEnumerable<DateTime> Range(this DateTime startDate, DateTime endDate)
        return Enumerable.Range(0, (endDate - startDate).Days + 1).Select(d => startDate.AddDays(d));

and use it like this

    var dates = new DateTime(2000, 1, 1).Range(new DateTime(2000, 1, 31));

Feel free to choose your own dates, you don’t have to restrict yourself to January 2000.

Method 4

Our resident maestro Jon Skeet has a great Range Class that can do this for DateTimes and other types.

Method 5

list = list.Where(s => s.startDate >= Input_startDate && s.endDate <= Input_endDate);


All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x