-1
import UIKit
import Foundation

let dateString = "2017-08-13T05:10:00Z"
let dateFormatter = ISO8601DateFormatter()

if var date = dateFormatter.date(from: dateString) {
    if let newYorkTimeZone = TimeZone(abbreviation: "EDT") {
        date.addTimeInterval(TimeInterval(newYorkTimeZone.secondsFromGMT(for: date)))
        print("NY time:", date, date.hour())
    }
}

if var date = dateFormatter.date(from: dateString) {
    if let chiTimeZone = TimeZone(abbreviation: "CDT") {
        date.addTimeInterval(TimeInterval(chiTimeZone.secondsFromGMT(for: date)))
        print("CHI time:", date, date.hour())
    }
}

if let gmt = TimeZone(abbreviation: "GMT") {
    if var date = dateFormatter.date(from: dateString) {
        print("GMT time:", date, date.hour())
    }
}

extension Date {
    func hour() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "h:mm a"

        return dateFormatter.string(from: self)
    }
}

当我的系统设置为 EDT 时输出:

纽约时间:2017-08-13 01:10:00 +0000 9:10 PM

CHI时间:2017-08-13 00:10:00 +0000 8:10 PM

格林威治标准时间:2017-08-13 05:10:00 +0000 1:10 AM


当我的系统设置为 CDT 时输出:

纽约时间:2017-08-13 01:10:00 +0000 8:10 PM

CHI时间:2017-08-13 00:10:00 +0000 7:10 PM

格林威治标准时间:2017-08-13 05:10:00 +0000 12:10 AM


无论我在哪个时区,这些时间不应该保持不变吗?我没有使用任何与TimeZone.current.

注意:我知道使用 是更好的做法TimeZone.secondsFromGMT:,但是使用 abbreviation 参数可以更容易地阅读此问题中发生的情况。

4

2 回答 2

0

考虑这段代码:

let now = Date()

let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .medium

func printDate(_ aDate: Date, inTimeZone timeZoneString: String? = nil) {
  let timeZone: TimeZone!
  if let timeZoneString = timeZoneString {
    timeZone = TimeZone(abbreviation: timeZoneString)
    guard timeZone != nil else {
      print("invalid time zone string \(timeZoneString)")
      return
    }
  } else {
    timeZone = TimeZone.current
  }
  formatter.timeZone = timeZone
  print("Date = \(formatter.string(from: now)) in time zone \(timeZone.abbreviation()!)")
}

printDate(now)
printDate(now, inTimeZone: "CDT")
printDate(now, inTimeZone: "GMT")
printDate(now, inTimeZone: "NZST")

在 EDT 中运行:输出为:

Date = Aug 12, 2017, 1:17:13 PM in time zone EDT
Date = Aug 12, 2017, 12:17:13 PM in time zone CDT
Date = Aug 12, 2017, 5:17:13 PM in time zone GMT
Date = Aug 13, 2017, 5:17:13 AM in time zone GMT+12
于 2017-08-12T17:20:54.067 回答
0

这是我在操场上的代码版本。

import UIKit

let dateString = "2017-08-13T05:10:00Z"
let isoDateFormatter = ISO8601DateFormatter()
guard let date = isoDateFormatter.date(from: dateString) else { assert(false) }

let gmtDateFormatter = DateFormatter()
gmtDateFormatter.timeZone = TimeZone(secondsFromGMT: 0)!
gmtDateFormatter.dateStyle = .medium
gmtDateFormatter.timeStyle = .medium

let nyDateFormatter = DateFormatter()
nyDateFormatter.timeZone = TimeZone(abbreviation: "EDT")!
nyDateFormatter.dateStyle = .medium
nyDateFormatter.timeStyle = .medium

let chiDateFormatter = DateFormatter()
chiDateFormatter.timeZone = TimeZone(abbreviation: "CDT")!
chiDateFormatter.dateStyle = .medium
chiDateFormatter.timeStyle = .medium

let gmtDate = gmtDateFormatter.string(from: date) // -> Aug 13, 2017, 5:10:00 AM
let nyDate = nyDateFormatter.string(from: date) // -> Aug 13, 2017, 1:10:00 AM
let chiDate = chiDateFormatter.string(from: date) // -> Aug 13, 2017, 12:10:00 AM

日期是一个固定的时间点,它没有时区或夏令时。要获取时间的字符串表示形式,请将aDateFormatter与应用的时区一起使用。我的 Playground 设置为默认的美国语言环境,但我可以更改任何日期格式化程序的语言环境以不同方式显示日期和时间。

您不需要为您拥有的日期添加间隔,您只需要格式化它的输出,这就是 DateFormatter 的用途。

于 2017-08-12T17:21:27.687 回答