let dateString = "2017-10-08T00:12:00.000Z"
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
let date = formatter.date(from: dateString)!
let components = Calendar.current.dateComponents(in: TimeZone(identifier: "America/New_York")!, from: date)
print("Day - \(components.day!)")
print("Month - \(components.month!)")
print("Year - \(components.year!)")
print("Hour - \(components.hour!)")
print("Min - \(components.minute!)")
print("Second - \(components.second!)")
print("Final Date - \(components.date!)")
通过运行上面的代码,我得到了输出
日 - 7 个
月 - 10
年 - 2017
小时 - 20分钟
- 12
秒 - 0
最终日期 - 2017-10-08 00:12:00 +0000
但我的预期日期应该是
最终日期 - 2017-10-07 20:12 :00 +0000