Search code examples
swiftnsdateformatter

Error while converting C# date string into swift3 date


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.


Solution

  • The correct date format is

    ddMMyyyyHHmmss