【问题标题】:How to convert date string with GMT and Time Zone, to Date如何将带有 GMT 和时区的日期字符串转换为日期
【发布时间】:2020-12-15 21:12:13
【问题描述】:

我似乎无法转换此日期字符串

2020 年 8 月 20 日星期四 13:50:00 GMT+0800(菲律宾标准时间)

到一个日期对象。我正在使用 AFDateHelper.swift 扩展,我正在使用这段代码来尝试解析它。

if let convertedDate = Date(fromString: dateString, format: .custom("EEE MMM d yyyy HH:mm:ss zzzz"), locale: Locale(identifier: "en_US_POSIX")) {
            return convertedDate
}

它不符合条件。

我也尝试过使用https://nsdateformatter.com/ 来检查我是否使用了正确的日期格式。这就是它给我的。

我错过了什么?

【问题讨论】:

    标签: swift date nsdateformatter


    【解决方案1】:

    更改日期格式:

            let dateString = "Thu Aug 20 2020 13:50:00 GMT+0800"
            let dateFormater = DateFormatter()
            dateFormater.dateFormat = "EEE MMM d yyyy HH:mm:ss 'GMT'zzzz"
            let date = dateFormater.date(from: dateString)
    
    

    这是输出:

    Printing description of date:
    ▿ Optional<Date>
      ▿ some : 2020-08-20 05:50:00 +0000
        - timeIntervalSinceReferenceDate : 619595400.0
    

    【讨论】:

      猜你喜欢
      • 2018-12-05
      • 2014-02-18
      • 2014-02-23
      • 1970-01-01
      • 2018-02-04
      • 2020-09-24
      • 2017-01-03
      • 1970-01-01
      相关资源
      最近更新 更多