C# 在一天中迭代几个小时

C# 在一天中迭代几个小时,c#,C#,具有日期时间变量的,例如: DateTime testDate = new DateTime(2011,12,15,00,00,00); 我如何为一天中的每一小时实现一个foreach循环 比如: foreach (int myHour in testDate.Date) { } 但这样就不会编译 使用代替: DateTime date = new DateTime(2011,12,15); for(int i = 0; i < 24; i++) { DateTime tim

具有日期时间变量的,例如:

DateTime testDate = new DateTime(2011,12,15,00,00,00);
我如何为一天中的每一小时实现一个foreach循环

比如:

foreach (int myHour in testDate.Date)
{

}

但这样就不会编译

使用
代替

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

对于那些不喜欢普通旧套圈的人:):


循环24小时不是一个好主意,因为这在有25或23小时的日子里不起作用(时间变化、夏令时…)

使用
AddHour
功能和目标日期

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);
}
更多信息

在一天中的所有24小时内迭代:

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

for (int i = 0; i < 24; i++)
{
    DateTime hour = testDate.Date.AddHours(i);
    // Your code here
}
DateTime testDate=newdatetime(2011,12,15);
对于(int i=0;i<24;i++)
{
DateTime小时=testDate.Date.AddHours(i);
//你的代码在这里
}

下面的代码允许您循环一天中的各个小时,但也可以从特定的小时开始。如果您不需要支持从小时偏移开始,那么它可能会更简单

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);
}
DateTime testDate=新的日期时间(2011,12,15,13,00,00);
var hourslef=24-testDate.Hour;
用于(变量小时=1;小时<小时左;小时++)
{
var nextDate=testDate.AddHours(小时);
控制台写入线(下一个日期);
}

foreach循环在列表中工作,但这里testDate.Date从不给出小时。因此,为了代替循环,可以使用while或while循环。

只需这样做即可

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

        }
DateTime testDate=新的日期时间(2011,12,15,10,00,00);
for(int i=testDate.Hour;i<24;i++)
{
//做什么
}
要及时获取小时数,请使用以下命令:

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);
}
DateTime testDate=newdatetime(2017,03,26,00,00,00,DateTimeKind.Local);
DateTime endDate=testDate.AddDays(1);
//这些日期也包含时间!
var start=TimeZone.CurrentTimeZone.GetDaylightChanges(testDate.Year).start;
var end=TimeZone.CurrentTimeZone.GetDaylightChanges(testDate.Year).end;
var hoursInDay=新列表();
while(testDate.Date!=endDate.Date)
{
如果(开始==测试日期)
{
//这一天有23个小时,应该跳过这一小时。
testDate=testDate.AddHours(1);
继续;
}
添加(测试日期);
if(end==testDate)
{
hoursInDay.Add(testDate);//这一天有25个小时。加上这个额外的小时
}
testDate=testDate.AddHours(1);
}
我在丹麦,所以当我运行它时,它只有23个小时。

DateTime today=DateTime.today;
DateTime today = DateTime.Today;
DateTime tomorrow = today.AddDays(1);
for ( var i = today; i <= tomorrow; i = i.AddHours(1))
{
    // your code
}
DateTime明天=今天。AddDays(1);
对于(var i=today;我注意到当您完成循环时testDate更改的副作用。@Justin这样做了?我很好奇。因为它必须知道时区才能这样做。@dknaack我尝试过,但没有。AFAIK文化与时区无关。“这在有25或23小时的日子里不起作用”-这是胡说八道。DateTime.AddHours没有考虑夏令时。@Stmated它没有修改原始日期。因此,在每个循环迭代开始时,
日期总是相同的。Justin在另一个答案中得出了一个很好的结论。不是所有的天都有24小时!!@Ray这取决于你所说的“天”是什么意思.Days由
System.DateTime
表示,它有24小时,因此
.AddDays(x)
.AddHours(x*24)
将始终具有相同的结果(实际上,它们都调用相同的
.Add()
方法,每个方法的乘数为24)因此,时间变化的历史案例和闰秒的案例需要比直接使用
日期时间
更复杂的东西。@JonHanna确实如此。如果这是为了在夏令时正确使用,则需要更复杂的解决方案。也可以将其LINQify…枚举范围(0,24).ForEach(hour=>testDate.AddHours(hour);…)@nonnb
IEnumerable
没有
ForEach
扩展方法。您必须先
ToList()
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);
}
DateTime today = DateTime.Today;
DateTime tomorrow = today.AddDays(1);
for ( var i = today; i <= tomorrow; i = i.AddHours(1))
{
    // your code
}