How to convert an Interval to a LocalDate range in NodaTime?

I'm running into a scenario where I need to convert an Interval value to Enumerable collection of LocalDate in NodaTime. How can I do that?

Below is the code

Interval invl = obj.Interval; 
//Here is the Interval value i.e.,{2016-10-20T00:00:00Z/2016-11-03T00:00:00Z}

How can I form a Date range between these intervals?

Thanks in advance.

Jon Skeet
people
quotationmark

A slightly alternative approach to the one given by Niyoko:

  • Convert both Instant values into LocalDate
  • Implement a range between them

I'm assuming that the interval is exclusive - so if the end point represents exactly midnight in the target time zone, you exclude that day, otherwise you include it.

So the method below includes every date which is covered within the interval, in the given time zone.

public IEnumerable<LocalDate> DatesInInterval(Interval interval, DateTimeZone zone)
{
    LocalDate start = interval.Start.InZone(zone).Date;
    ZonedDateTime endZonedDateTime = interval.End.InZone(zone);
    LocalDate end = endLocalDateTime.Date;
    if (endLocalDateTime.TimeOfDay == LocalTime.Midnight)
    {
        end = end.PlusDays(-1);
    }
    for (LocalDate date = start; date <= end; date = date.PlusDays(1))
    {
        yield return date;
    }
}

people

See more on this question at Stackoverflow