Chris Conway
Chris Conway

Reputation: 16529

How do you iterate through every day of the year?

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!

Upvotes: 39

Views: 17976

Answers (8)

OscarRyz
OscarRyz

Reputation: 199274

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 :)

Upvotes: 23

healsjnr
healsjnr

Reputation: 405

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.

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1502386

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);
}

Upvotes: 5

Samantha Branham
Samantha Branham

Reputation: 7451

I would use a loop that looks like this

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

Set begin and end accordingly

Upvotes: 86

kemiller2002
kemiller2002

Reputation: 115518

DateTime dateTime = new DateTime(2009, 1, 1);    

while(dateTime.Year < 2010)
    {

      dateTime = dateTime.AddDays(1);
    }

Upvotes: 7

DMCS
DMCS

Reputation: 31880

DateTime current = DateTime.Parse("1/1/2009");
DateTime nextYear = current.AddYears(1);
do
{
    Console.WriteLine(current);
    current = current.AddDays(1);
} while (current < nextYear) ;

Upvotes: 0

jgallant
jgallant

Reputation: 11273

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.

Upvotes: 4

Robert S.
Robert S.

Reputation: 25294

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

Sorry, couldn't resist.

Upvotes: 2

Related Questions