Why I can't convert string to date in Swift? [duplicate]

南笙酒味 提交于 2019-12-08 13:57:52

问题


I have this simple code:

let string = "2017-02-23 15:26:00";
let df = DateFormatter();
df.dateFormat = "yyyy-MM-dd hh:mm:ss";
let result = df.date(from: string);

The result is nil. Why?


回答1:


Your date is 24 hour format So it should be HH not hh, so change your date formate to yyyy-MM-dd HH:mm:ss.

let string = "2017-02-23 15:26:00"
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss"
let result = df.date(from: string)



回答2:


let string = "2017-02-23 15:26:00";
let df = DateFormatter();
df.dateFormat = "yyyy-MM-dd HH:mm:ss";
let result = df.date(from: string);


来源:https://stackoverflow.com/questions/42502568/why-i-cant-convert-string-to-date-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!