For ... Next with DateTime

I must have a blackout.
What I want is to loop from a BeginDate to an EndDate with a 1 day step.
Can somebody point me how looping within For … Next is done when with DateTime fields?

I have searched the documentation but I couldn’t find information on how the combination For … Next and DateTime is done.
Searched the example projects but also no result.

Var begindate As DateTime = DateTime.Now
Var current As DateTime = begindate
Var endDate As DateTime

While (current < enddate)

// your code

current = current.AddInterval(0, 0, 1)

Not sure about the for … next but the above should do the trick

1 Like

Something like this?:

for i as Integer = BeginDate.SecondsFrom1970 to EndDate.SecondsFrom1970 step onedaystep

onedaystep is the seconds in one day

1 Like

You can accomplish the For Next with the number of days you want in your loop and the DateTime.Interval adds a day for each loop step. For whichever method you use for a loop the secret here is the DateTime.Interval between your start and end dates.

You could implement an iterator:


So what about two months then ?

Thank you. That was the solution I was looking for.

Thank you for your example.
I have to work with the date within the loop.
Constantly switching between SecondsFrom1970 and DateTime isn’t what I prefer.

Thank you for the link to the blog.
I have read the blog and downloaded the project.
It’s a very nice way of iterating a date range.
For now it’s overkill for what I want to achieve but I will certainly adapt and use it in the near future.

1 Like