我有一个时间戳字符串,其格式为:
2018-03-06T06:28:39.887Z
并且需要将其格式化为更具人性化的内容,我尝试了以下日期格式,但日期不解析
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "y-MM-ddTk:mm:ss.SSSZ"
let date = dateFormatter.date(from: "2018-03-06T06:28:39.887Z")
我的日期格式缺少什么?
答案 0 :(得分:-2)
Swift 3/4解决方案:
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"
if let date = dateFormatterGet.date(from: "2016-02-29 12:24:26"){
print(dateFormatterPrint.string(from: date))
}
else {
print("There was an error decoding the string")
}