How can I use datetime in for loop?
How can I use datetime
in for
loop ?
There are two variables duedate
and returndate
, return date is current date开发者_如何转开发 and duedate is incremented by one day and is equal to returndate.
How can I use this in for loop?
You could use:
DateTime start = ...;
DateTime finish = ...;
for (DateTime x = start; x <= finish; x = x.AddDays(1))
{
... // use x
}
for(DateTime date=duedate;date.Date<DateTime.Now.Date;date=date.AddDays(1))
{
}
something like this
It will be pretty straight. See below:
DateTime validFrom = (DateTime)dtValidFrom.Value;
DateTime validTo = (DateTime)dtValidTo.Value;
for (DateTime dt = validFrom; dt <= validTo; dt = dt.AddDays(1))
{
}
This should get you started:
DateTime end = new DateTime();
for (DateTime start = new DateTime(); start < end; start.AddDays(1))
{
//process
}
An other way:
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
}
Or:
for (DateTime t = start; t <= endval; t += incrementval)
{
//Your code will reach endval
}
精彩评论