这种带T的时间戳是按照ISO8601标准定义的,包含了时区信息,处理的时候需要定义好当前时区,否则会有问题。
public func ISO8601TimeToDate()->Date?{
let dateFormatter = ISO8601DateFormatter()
dateFormatter.timeZone = TimeZone(identifier: "Asia/Shanghai")
dateFormatter.formatOptions = [.withFullDate, .withTime,.withDashSeparatorInDate,.withColonSeparatorInTime]
guard let date = dateFormatter.date(from: self) else {return nil}
return date
}
另外DateComponents也是包含时区信息,两个dateComponents的year,month,day,year,month,second,nanosecond完全一样也可能代表不同的时间,有问题的时候打印出来看一下就会发现。