如何在 datetime
循环中使用for
?
有两个变量duedate
和returndate
,返回日期是当前日期,duedate增加一天,等于returnndate。
如何在for循环中使用它?
答案 0 :(得分:11)
您可以使用:
DateTime start = ...;
DateTime finish = ...;
for (DateTime x = start; x <= finish; x = x.AddDays(1))
{
... // use x
}
答案 1 :(得分:2)
for(DateTime date=duedate;date.Date<DateTime.Now.Date;date=date.AddDays(1))
{
}
类似这样的事情
答案 2 :(得分:1)
这将非常直接。见下文:
DateTime validFrom = (DateTime)dtValidFrom.Value;
DateTime validTo = (DateTime)dtValidTo.Value;
for (DateTime dt = validFrom; dt <= validTo; dt = dt.AddDays(1))
{
}
答案 3 :(得分:0)
这应该让你开始:
DateTime end = new DateTime();
for (DateTime start = new DateTime(); start < end; start.AddDays(1))
{
//process
}
答案 4 :(得分:0)
另一种方式:
DateTime start = new DateTime();
DateTime endval = new DateTime();
//It means it is 1 hour interval:
TimeSpan inctrementval = new TimeSpan(1, 0, 0);
for (DateTime t = start; t < endval; t += incrementval)
{
//Your code will not reach endval
}
或者:
for (DateTime t = start; t <= endval; t += incrementval)
{
//Your code will reach endval
}