Siva
Siva

Reputation: 581

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?

Upvotes: 1

Views: 9759

Answers (5)

Tarun Pahuja
Tarun Pahuja

Reputation: 61

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))
{

}

Upvotes: 1

etveszprem
etveszprem

Reputation: 141

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
}

Upvotes: 0

Rob Stone
Rob Stone

Reputation: 204

This should get you started:

DateTime end = new DateTime();

for (DateTime start = new DateTime(); start < end; start.AddDays(1))
{
    //process
}

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273199

You could use:

DateTime start = ...;
DateTime finish = ...;

for (DateTime x = start; x <= finish; x = x.AddDays(1))
{
   ... // use x
}

Upvotes: 11

Valentin Kuzub
Valentin Kuzub

Reputation: 12073

for(DateTime date=duedate;date.Date<DateTime.Now.Date;date=date.AddDays(1))
{
}

something like this

Upvotes: 2

Related Questions