I'm running into a scenario where I need to convert an
Interval
value to Enumerable collection ofLocalDate
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.
A slightly alternative approach to the one given by Niyoko:
Instant
values into LocalDate
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;
}
}
See more on this question at Stackoverflow