39

Given a start date of 1/1/2009 and an end date of 12/31/2009, how can I iterate through each date and retrieve a DateTime value using c#?

Thanks!

Chris Conway
  • 16,269
  • 23
  • 96
  • 113

8 Answers8

85

I would use a loop that looks like this

for(DateTime date = begin; date <= end; date = date.AddDays(1))
{
}

Set begin and end accordingly

TK.
  • 46,577
  • 46
  • 119
  • 147
Samantha Branham
  • 7,350
  • 2
  • 32
  • 44
23

Another option implementing the Iterator design pattern:

This may sound unnecessary, but I depending on how to you use this functionality, you may also implement the Iterator design pattern.

Think on this. Suppose that everything works just fine, and you copy/paste all over the place the "for" sentence. And suddenly as part of the requirements, you have to iterate all the days but skip some of them ( like in calendar, skip holydays, weekends, custom etc. )

You would have to create a new "snipped" and use Calendar instead. Then search and replace all your for's.

In OOP, this could be achieved using the Iterator pattern.

From Wikpedia:

In object-oriented programming, the Iterator pattern is a design pattern in which iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation. An Iterator object encapsulates the internal structure of how the iteration occurs.

So the idea is to use a construct like this:

        DateTime fromDate = DateTime.Parse("1/1/2009");
        DateTime toDate   = DateTime.Parse("12/31/2009");

        // Create an instance of the collection class
        DateTimeEnumerator dateTimeRange = 
                              new DateTimeEnumerator( fromDate, toDate );


        // Iterate with foreach
        foreach (DateTime day in dateTimeRange )
        {
            System.Console.Write(day + " ");
        }

And then if needed you could create subclasses to implement different algorithms, one that uses AddDay(1), other that uses AddDay( 7 ) or other that simple uses Calendar instead. Etc. etc.

The idea is to lower the coupling between objects.

Again, this would be overkill for most of the cases, but if the iteration forms a relevant part of a system ( let's say , you are creating some kind of whatever notification, for an enterprise, and should adhere to different globalizations

The basic implementation of course would use the for.

public class DateTimeEnumerator : System.Collections.IEnumerable
{
    private DateTime begin;
    private DateTime end;

    public DateTimeEnumerator ( DateTime begin , DateTime end ) 
    {
        // probably create a defensive copy here... 
        this.begin = begin;
        this.end = end;
    }
    public System.Collections.IEnumerator GetEnumerator()
    {
        for(DateTime date = begin; date < end; date = date.AddDays(1))
        {
            yield return date;
        }
    }
}

Just an idea :)

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
7
DateTime dateTime = new DateTime(2009, 1, 1);    

while(dateTime.Year < 2010)
    {

      dateTime = dateTime.AddDays(1);
    }
kemiller2002
  • 113,795
  • 27
  • 197
  • 251
  • 2
    Add days returns a DateTime rather than increasing the DateTime it operates on. – Ray Feb 10 '09 at 19:25
5

I'd use MiscUtil and its extension methods:

foreach(DateTime date in 1.January(2009)
                         .To(31.December(2009))
                         .Step(1.Days())
{
    Console.WriteLine(date);
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
4

Set two variables:

DateTime lowValue = DateTime.Parse("1/1/2009");
DateTime highValue = DateTime.Parse("12/31/2009");

Then, add a day to the low value until it is equal to highvalue:

while (lowValue <= highValue)
{
   //Do stuff here
   lowValue = lowValue.AddDays(1);
}

Or something like that.

John Sheehan
  • 77,456
  • 30
  • 160
  • 194
jgallant
  • 11,143
  • 1
  • 38
  • 72
3

An alternative method that might be more reusable is to write an extension method on DateTime and return an IEnumerable.

For example, you can define a class:

public static class MyExtensions
{
    public static IEnumerable EachDay(this DateTime start, DateTime end)
    {
        // Remove time info from start date (we only care about day). 
        DateTime currentDay = new DateTime(start.Year, start.Month, start.Day);
        while (currentDay <= end)
        {
            yield return currentDay;
            currentDay = currentDay.AddDays(1);
        }
    }
}

Now in the calling code you can do the following:

DateTime start = DateTime.Now;
DateTime end = start.AddDays(20);
foreach (var day in start.EachDay(end))
{
    ...
}

Another advantage to this approach is that it makes it trivial to add EachWeek, EachMonth etc. These will then all be accessible on DateTime.

healsjnr
  • 405
  • 3
  • 10
2

int day; for (int i = 1; i<365;i++) { day++; }

Sorry, couldn't resist.

Robert S.
  • 25,266
  • 14
  • 84
  • 116
0
DateTime current = DateTime.Parse("1/1/2009");
DateTime nextYear = current.AddYears(1);
do
{
    Console.WriteLine(current);
    current = current.AddDays(1);
} while (current < nextYear) ;
DMCS
  • 31,720
  • 14
  • 71
  • 104