R
R
root092016-02-06 02:36:13
C++ / C#
root09, 2016-02-06 02:36:13

How to loop through all days between two dates?

For example, there is a date: 01/29/2016 and there is a second date 02/03/2016 , you need to run through the days between these dates, how can this be done? i.e. in the loop
you need to get these dates:
29.01.2016
30.01.2016
31.01.2016
01.02.2016
02.02.2016

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
root09, 2016-02-06
@root09

Found it myself:

public IEnumerable<DateTime> EachDay(DateTime from, DateTime thru)
        {
            for (var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1))
                yield return day;
        }
.....
foreach (DateTime day in EachDay(StartDate, EndDate))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question