Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 313
Bilal Avatar asked Nov 13 '14 15:11

Bilal


2 Answers

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.

like image 186
Bathsheba Avatar answered Oct 26 '22 04:10

Bathsheba


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)) { ... }
like image 4
Kristof Claes Avatar answered Oct 26 '22 05:10

Kristof Claes