ios - 来自字符串时区的日期不符合使用 DateFormatter

标签 ios swift timezone dateformatter

代码:

let isoDate = "1981-02-20T10:44:00+0800"
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"

var localTimeZoneAbbreviation: String { return TimeZone.current.abbreviation() ?? "" }

let date = dateFormatter.date(from: isoDate)!
dateFormatter.string(from: date)

输出: enter image description here

为什么日期输出时区是 +07:30 而不是日期字符串和 TimeZone.current 中定义的 +0800?谁能解释一下?

最佳答案

时区格式不是Z而是ZZZ

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZ"

关于ios - 来自字符串时区的日期不符合使用 DateFormatter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45584841/

相关文章:

ios - 从 IBAction 以编程方式设置 UISwitch isOn 再次调用 IBAction

swift - 困惑为什么 Realm LinkingObjects() 不起作用 :(

ios - iPhone 未从 Apple Watch 接收应用程序上下文

asp.net - 不同国家的时区

objective-c - 非 ARC block ,内存泄漏问题

ios - 如何创建对对象的引用以在 iOS 中为其设置动画

Swift 在结构中内存/缓存惰性变量

java - Java 中的 XMLGregorianCalendar,没有时区

apache-spark - PySpark to_utc_timestamp 返回相同时间

ios - UITableViewCell 内的 UIWebView 每次滚动时都会加载内容