c# – 在一天内完成几个小时的迭代
发布时间:2020-12-15 20:00:06 所属栏目:百科 来源:网络整理
导读:有一个DateTime变量,例如: DateTime testDate = new DateTime(2011,12,15,00,00); 如何在今天的每个小时实现一个foreach循环? 就像是: foreach (int myHour in testDate.Date){} 但是这种方式不能编译. 解决方法 循环24并不是一个好主意,因为这在25或23小
有一个DateTime变量,例如:
DateTime testDate = new DateTime(2011,12,15,00,00); 如何在今天的每个小时实现一个foreach循环? 就像是: foreach (int myHour in testDate.Date) { } 但是这种方式不能编译. 解决方法
循环24并不是一个好主意,因为这在25或23小时(时间变化,夏令时……)的日子里不起作用.
使用AddHour函数和目标日期. DateTime testDate = new DateTime(2011,DateTimeKind.Local); DateTime endDate = testDate.AddDays(1); while (testDate.Date != endDate.Date) { Console.WriteLine(testDate.ToString()); testDate = testDate.AddHours(1); } 更多信息 > MSDN – DateTimeKind Enumeration (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |