Error while converting C# date string into swift3

2019-09-21 17:56发布

In one of my app i am getting date from an RESTful api as "04112017182149".

I tried to convert it into swift date for my internal user as shown in below snippet.

let receivedDate = "04112017182149"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd" 
print((dateFormatter.date(from: receivedDate))!)

It is throwing a fatal error as Unexpectedly found nil while unwrapping optional value.

I had tried to change date formatter string as "dd-MMM-yyyy HH:mm:ss" but no use.

1条回答
一纸荒年 Trace。
2楼-- · 2019-09-21 18:25

The correct date format is

ddMMyyyyHHmmss
查看更多
登录 后发表回答