Itrarting Bewteen Date Range using Foreach C#.

There are some situation when we want to itrate trough a range of date.
Hear is ample how you can itrates through the date range using foreach in C#

for itrating you will need to first create a function which will convert the date range in inumaration formate so that you can itrates.

For this i have created the follwing function.

public IEnumerable<DateTime> EachDay(DateTime from, DateTime thru)
for (var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1)) yield return day;

Now you can use the code like this.

foreach (DateTime day in EachDay(strartDate, EndDate)
 //Do something


Post a Comment

Previous Post Next Post