我遵循2020-07-15T12:32:38+00:00
之类的UTC。实际上,我需要考虑到时区,将此字符串转换为电话中的本地日期。这是我从其他stackoverflow答案中看到的尝试:
func UTCToLocal(date: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
//I thought than below can help
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
return dateFormatter.date(from: date)
}
另一种方法:
func UTCToLocal2(date: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
/// I thought that specifying current time zone would help
dateFormatter.timeZone = TimeZone.current
return dateFormatter.date(from: date)
}
但是没有一种方法可行。我的2020-07-15T12:32:38+00:00
未转换为我所在时区的日期。我的错误在哪里?