如何在给定时区中获得与特定时间相匹配的日期 - Swift
如何获得与给定时区中给定时间匹配的date
对象?
例如,我想获得代表下一个CST的date
。
我想到了这一点,但是我不确定这是否是最好的方法(如果它根本无法使用)。我也不知道它是否会生成与给定数据组件相匹配的下一个最早日期。苹果文档对此有些不清楚。
let calendar = Calendar(identifier: .gregorian)
let timeZone = TimeZone(abbreviation: "CDT")
let dateComponents = DateComponents(calendar: calendar, timeZone: timeZone, hour: 3)
let date3AM = calendar.date(from: dateComponents)
How can I get a Date
object that matches a given time in a given time zone?
For example, I want to get the Date
that represents the next 3AM CST.
I thought of this, but I'm not sure if it's the best way (if it even works at all). I also don't know if it generates the next soonest date that matches the given data components. The Apple docs are a little unclear on this.
let calendar = Calendar(identifier: .gregorian)
let timeZone = TimeZone(abbreviation: "CDT")
let dateComponents = DateComponents(calendar: calendar, timeZone: timeZone, hour: 3)
let date3AM = calendar.date(from: dateComponents)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,有一种更简单的方法。
In fact, there is an easier way.