使用此代碼:
var l = Enumerable.Range(0, int.MaxValue)
.Select(x => Period.FromDays(x))
.Select(x => LocalDate.Add(interval.Start.InZone(localZone).Date, x))
.TakeWhile(x => x.CompareTo(interval.End.InZone(localZone).Date) <= 0);
實施例:
var localZone = DateTimeZone.ForOffset(Offset.FromHours(7));
var start = Instant.FromDateTimeOffset(new DateTimeOffset(new DateTime(2016, 10, 1)));
var end = Instant.FromDateTimeOffset(new DateTimeOffset(new DateTime(2016, 10, 25)));
var interval = new Interval(start, end);
var l = Enumerable.Range(0, int.MaxValue)
.Select(x => Period.FromDays(x))
.Select(x => LocalDate.Add(interval.Start.InZone(localZone).Date, x))
.TakeWhile(x => x.CompareTo(interval.End.InZone(localZone).Date) <= 0);
foreach (var localDate in l)
{
Console.WriteLine(localDate);
}
謝謝。我會試試你的解決方案。此外,我收到一個錯誤,如'不能隱式轉換類型'NodaTime.ZonedDateTime'到'NodaTime.LocalDateTime'。將LocalDateTime轉換爲ZonedDateTime解決了錯誤。我做對了嗎? – Karthik
@Karthik:是的 - 現在修好了樣品,謝謝。 –
謝謝@Jon Skeet。 – Karthik