Error while converting C# date string into swift3

2019-09-21 18:07发布

问题:

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:

The correct date format is

ddMMyyyyHHmmss