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

2019-06-14 17:06发布

This question already has an answer here:

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?

2条回答
劫难
2楼-- · 2019-06-14 17:42

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)
查看更多
看我几分像从前
3楼-- · 2019-06-14 17:43
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);
查看更多
登录 后发表回答