AddDays () for the loop

I want to use the AddDays () method for a loop. But that will not work. despite being used in a loop, the value of the day does not increase. Then it transforms an infinite loop. For instance:

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 do I use the AddDays () method in a for loop

Thank you very much

+5
source share
2 answers

dt.AddDays(1) returns the new object that you are dropping.

You can use dt = dt.AddDays(1) in a for loop instead of what you have.

+14
source

AddDays methods return a new DateTime , so your dt object will never be modified. However, you can reassign it. This should work:

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

Source: https://habr.com/ru/post/1206814/


All Articles