你必须一个接一个地尝试这两种格式:
func ISOStringToLocal(_ dateString: String) -> String {
let dateFormatterGet = DateFormatter()
dateFormatterGet.timeZone = TimeZone(identifier: "UTC")
dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ" // with seconds
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "dd-MMM-yyyy hh:mm:ss"
if let date = dateFormatterGet.date(from: dateString) {
return dateFormatterPrint.string(from: date)
} else {
dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ss:SSSZZZZZ" // with millis
if let date = dateFormatterGet.date(from: dateString) {
return dateFormatterPrint.string(from: date)
}
return ""
}
}
print(ISOStringToLocal("2017-07-01T13:24:00+02:00"))
print(ISOStringToLocal("2017-07-01T13:24:00:123+02:00"))