C# 如何在NodeTime中将时间间隔转换为LocalDate范围?

C# 如何在NodeTime中将时间间隔转换为LocalDate范围?,c#,.net,nodatime,C#,.net,Nodatime,我遇到了一个场景,需要将间隔值转换为NodaTime中LocalDate的可枚举集合。我该怎么做 下面是代码 Interval invl = obj.Interval; //Here is the Interval value i.e.,{2016-10-20T00:00:00Z/2016-11-03T00:00:00Z} 如何在这些时间间隔之间形成日期范围 提前感谢。使用以下代码: var l = Enumerable.Range(0, int.MaxValue)

我遇到了一个场景,需要将
间隔
值转换为NodaTime中
LocalDate
的可枚举集合。我该怎么做

下面是代码

Interval invl = obj.Interval; 
//Here is the Interval value i.e.,{2016-10-20T00:00:00Z/2016-11-03T00:00:00Z}
如何在这些时间间隔之间形成日期范围

提前感谢。

使用以下代码:

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 l=Enumerable.Range(0,int.MaxValue)
.选择(x=>Period.FromDays(x))
.Select(x=>LocalDate.Add(interval.Start.InZone(localZone.Date,x))
.TakeWhile(x=>x.CompareTo(interval.End.InZone(localZone.Date))Period.FromDays(x))
.Select(x=>LocalDate.Add(interval.Start.InZone(localZone.Date,x))
.TakeWhile(x=>x.CompareTo(interval.End.InZone(localZone.Date))使用以下代码:

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 l=Enumerable.Range(0,int.MaxValue)
.选择(x=>Period.FromDays(x))
.Select(x=>LocalDate.Add(interval.Start.InZone(localZone.Date,x))
.TakeWhile(x=>x.CompareTo(interval.End.InZone(localZone.Date))Period.FromDays(x))
.Select(x=>LocalDate.Add(interval.Start.InZone(localZone.Date,x))

.TakeWhile(x=>x.CompareTo(interval.End.InZone(localZone.Date))一种与Niyoko给出的方法稍有不同的方法:

  • 将两个
    Instant
    值转换为
    LocalDate
  • 在它们之间实现一个范围
我假设时间间隔是独占的——因此,如果终点正好代表目标时区的午夜,则排除该天,否则包括它

因此,下面的方法包括给定时区内间隔内的每个日期

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;
    }
}
public IEnumerable DatesInInterval(间隔时间,日期时区)
{
LocalDate start=interval.start.InZone(zone.Date);
ZonedDateTime endZonedDateTime=区间.End.InZone(区域);
LocalDate end=endLocalDateTime.Date;
if(endLocalDateTime.TimeOfDay==LocalTime.Midnight)
{
结束=结束多个星期(-1);
}

对于(LocalDate=start;date一种与Niyoko给出的方法稍有不同的方法:

  • 将两个
    Instant
    值转换为
    LocalDate
  • 在它们之间实现一个范围
我假设时间间隔是独占的——因此,如果终点正好代表目标时区的午夜,则排除该天,否则包括它

因此,下面的方法包括给定时区内间隔内的每个日期

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;
    }
}
public IEnumerable DatesInInterval(间隔时间,日期时区)
{
LocalDate start=interval.start.InZone(zone.Date);
ZonedDateTime endZonedDateTime=区间.End.InZone(区域);
LocalDate end=endLocalDateTime.Date;
if(endLocalDateTime.TimeOfDay==LocalTime.Midnight)
{
结束=结束多个星期(-1);
}

对于(LocalDate date=start;date谢谢。您的解决方案运行良好。但您能否解释DateTimeZone对象
localZone
?,因为它设置了一个偏移值。@Karthik它用于将
Instant
转换为
LocalDate
,因为
Instant
与时区无关。请根据您的本地时区更改它。或者您可以n通过调用
DateTimeZoneProviders.Bcl.GetSystemDefault()自动获取本地时区
谢谢@Niyoko Yuliawan.谢谢。你的解决方案很好。但是你能解释一下DateTimeZone对象
localZone
?,因为它设置了一个偏移值。@Karthik它用于将
Instant
转换为
LocalDate
,因为
Instant
是时区不可知的。根据你的本地时区更改它。或者你可以通过调用
DateTimeZoneProviders.Bcl.GetSystemDefault()自动获取本地时区
谢谢@Niyoko Yuliawan.谢谢。我会用你的解决方案试试。另外,我遇到了一个错误,比如
无法将类型'NodaTime.ZonedDateTime'隐式转换为'NodaTime.LocalDateTime'
。将
LocalDateTime
转换为
ZonedDateTime
解决了这个错误。我这样做对吗?谢谢@Jon Skeet另外,我遇到了一个错误,比如
无法将类型“NodaTime.ZonedDateTime”隐式转换为“NodaTime.LocalDateTime”
。将
LocalDateTime
转换为
ZonedDateTime
解决了这个错误。我这样做对吗?谢谢@Jon Skeet。