Bilal
Bilal

Reputation: 159

AddDays() in for loop

I want to use AddDays() method in for loop. But it doesn't work. in spite of using in loop, day value is not increasing. Then it is transforming infinite loop. For example;

DateTime exDt = tempPermissionWarning[i].planned_start_date;
for (DateTime dt = exDt; dt <= newTo; dt.AddDays(1))
{
    context = context + dt.ToShortDateString() + "æ" + tempPermissionWarning[i].resource_name) + ¨";
}

How I use AddDays() method in a for loop

Thank you so much

Upvotes: 5

Views: 2184

Answers (2)

Kristof Claes
Kristof Claes

Reputation: 10941

The AddDays methods returns a new DateTime, so your dt object never gets modified. You can reassign it however. This should work:

for (DateTime dt = exDt; dt <= newTo; dt = dt.AddDays(1)) { ... }

Upvotes: 4

Bathsheba
Bathsheba

Reputation: 234635

dt.AddDays(1) returns a new object, which you are discarding.

You could use dt = dt.AddDays(1) in the for loop, in place of what you currently have.

Upvotes: 16

Related Questions