【发布时间】:2017-09-14 10:34:03
【问题描述】:
我想将日期从本地转换为 UTC 并将 UTC 转换为本地。
例如。 2015 年 4 月 1 日下午 12:00 是 UTC,然后
2015 年 4 月 1 日下午 5:30 对我来说是本地时间,但我正在反转它
let dateFormatter2 = DateFormatter()
dateFormatter2.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"[![enter image description here][1]][1]
dateFormatter2.timeZone = NSTimeZone.local
let date2 = dateFormatter2.date(from: "2015-04-01T12:00:00")
dateFormatter2.timeZone = NSTimeZone(name: "UTC")! as TimeZone
let date3 = dateFormatter2.date(from: "2015-04-01T12:00:00")
我得到了 2015 年 4 月 1 日下午 5:30 作为 UTC 和 2015 年 4 月 1 日下午 12:00 本地
【问题讨论】:
-
当有原生 Swift 替代品时,不要在 Swift 中使用 Foundation 类型。使用
TimeZone而不是NSTimeZone。