我想从字符串转换为日期。我在这里使用正确的格式 吗? "yyyy-MM-dd'T'HH:mm:ss+00:00"?
extension String {
func format(_ format:String) -> String {
var date: Date?
let dateFormatterGet = DateFormatter()
let dateFormatterOutput = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ss+00:00"
dateFormatterOutput.dateFormat = format
if let d = dateFormatterGet.date(from: self) {
date = d
}
return dateFormatterOutput.string(from: date!)
}
}
extension Date {
func string(with format: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.string(from: self)
}
}