Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over each Day between StartDate and EndDate [duplicate]

Tags:

c#

.net

datetime

I have a DateTime StartDate and EndDate.

How can I, irrespective of times, iterate across each Day between those two?

Example: StartDate is 7/20/2010 5:10:32 PM and EndDate is 7/29/2010 1:59:12 AM.

I want to be able to iterate across 7/20, 7/21, 7/22 .. 7/29.

like image 654
Alex Avatar asked Jul 29 '10 05:07

Alex


1 Answers

for(DateTime date = StartDate; date.Date <= EndDate.Date; date = date.AddDays(1)) {     ... } 

The .Date is to make sure you have that last day, like in the example.

like image 149
Yuriy Faktorovich Avatar answered Sep 25 '22 07:09

Yuriy Faktorovich