Ciupaz
Ciupaz

Reputation: 699

Iterate through hours in a day

having a DateTime variable, for example:

DateTime testDate = new DateTime(2011,12,15,00,00,00);

how can I implement a foreach loop for every hour of this day?

Something like:

foreach (int myHour in testDate.Date)
{

}

but in this way does not compile.

Upvotes: 5

Views: 8452

Answers (9)

Yoe
Yoe

Reputation: 7

DateTime today = DateTime.Today;
DateTime tomorrow = today.AddDays(1);
for ( var i = today; i <= tomorrow; i = i.AddHours(1))
{
    // your code
}

Upvotes: 0

Jens Kloster
Jens Kloster

Reputation: 11277

To get the hours in DLS time use this:

DateTime testDate = new DateTime(2017, 03, 26, 00, 00, 00, DateTimeKind.Local);
DateTime endDate = testDate.AddDays(1);

//these dates also contain time!
var start = TimeZone.CurrentTimeZone.GetDaylightChanges(testDate.Year).Start; 
var end = TimeZone.CurrentTimeZone.GetDaylightChanges(testDate.Year).End;

var hoursInDay = new List<DateTime>();

while (testDate.Date != endDate.Date)
{
    if (start == testDate)
    {
        //this day have 23 hours, and should skip this hour.
        testDate = testDate.AddHours(1);
        continue; 
    }

    hoursInDay.Add(testDate);

    if (end == testDate)
    {
        hoursInDay.Add(testDate); //this day has 25 hours. add this extra hour
    }

    testDate = testDate.AddHours(1);
}

I'm in Denmark, so when I run this it has only 23 hours.

Upvotes: 1

dknaack
dknaack

Reputation: 60486

It is not a good idea to loop 24, because this will not work on days with 25 or 23 hours (time change, daylight saving...).

Use the AddHour function and a target date.

DateTime testDate = new DateTime(2011, 12, 15, 00, 00, 00, DateTimeKind.Local);
DateTime endDate = testDate.AddDays(1);

while (testDate.Date != endDate.Date)
{
    Console.WriteLine(testDate.ToString());
    testDate = testDate.AddHours(1);
}

More Information

Upvotes: 16

Bobby Cannon
Bobby Cannon

Reputation: 6913

The code below allows you to cycle through the hours of the day but also starting from a specific hour. It could be simpler if you do not need to support starting from an hour offset.

DateTime testDate = new DateTime(2011,12,15,13,00,00);
var hoursLeft = 24 - testDate.Hour;

for (var hour = 1; hour < hoursLeft; hour++)
{
    var nextDate = testDate.AddHours(hour);
    Console.WriteLine(nextDate);
}

Upvotes: 1

Zia
Zia

Reputation: 921

simply do this

DateTime testDate = new DateTime(2011, 12, 15, 10, 00, 00);
        for (int i = testDate.Hour; i < 24; i++)
        {
            //do what ever

        }

Upvotes: -1

Dewasish Mitruka
Dewasish Mitruka

Reputation: 2896

foreach loop works in list but here testDate.Date never gives you hour. so in substitution of it use for loop or do while or while loop.

Upvotes: 1

Phil Klein
Phil Klein

Reputation: 7514

Iterate over all 24 hours of the day:

DateTime testDate = new DateTime(2011, 12, 15);

for (int i = 0; i < 24; i++)
{
    DateTime hour = testDate.Date.AddHours(i);
    // Your code here
}

Upvotes: 0

Jan
Jan

Reputation: 16042

For those who don't like plain old for loops :) :

DateTime testDate = new DateTime(2011,12,15,00,00,00);
foreach (int hour in Enumerable.Range(0,24)) {
    DateTime dateWithHour = testDate.AddHours(hour);
}

Upvotes: 1

Thomas Levesque
Thomas Levesque

Reputation: 292445

Use for instead:

DateTime date = new DateTime(2011,12,15);
for(int i = 0; i < 24; i++)
{
    DateTime time = date.AddHours(i);
    ...
}

If you really want to use foreach, you could create an extension method like this:

static class DateTimeExtensions
{
    public static IEnumerable<DateTime> GetHours(this DateTime date)
    {
        date = date.Date; // truncate hours
        for(int i = 0; i < 24; i++)
        {
            yield return date.AddHours(i);
        }
    }
}

...

DateTime date = new DateTime(2011,12,15);
foreach (DateTime time in date.GetHours())
{
    ...
}

Upvotes: 6

Related Questions