【发布时间】:2019-07-03 07:20:33
【问题描述】:
我正在尝试为下一个发生的未来时间获取 Date 对象,其中 UTC 时间的小时为 18。但是我的代码无法按预期工作。我有以下内容:
let dateComponents = DateComponents(timeZone: TimeZone(abbreviation: "GMT"), hour: 18)
let date = Calendar.current.nextDate(after: Date(), matching: dateComponents, matchingPolicy: .nextTime)
print(date)
问题是这会导致2019-02-09 23:00:00 +0000
日期是EST 中的下一个发生时间,小时为 18。
由于dateComponents 的时区设置为 UTC,小时设置为 18,因此我预计日期将是 2019-02-09 18:00:00 +0000。此外,更改时区似乎对找到的nextDate 没有影响。
为什么 nextDate 函数不尊重传递给它的 dateComponents 中设置的时区?
【问题讨论】:
标签: swift date calendar timezone