Martin Muldoon
Martin Muldoon

Reputation: 3438

Can't convert date/time string into date

The conversion from string to date keeps returning nil:

if let dateString = dict["dateScanned"] as? String {
       let strTime = dateString // 9/5/2017 12:00:00 AM
       let formatter = DateFormatter()
       formatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
       let myDate = formatter.date(from: strTime)  // myDate = nil
       print(myDate)
   }

Upvotes: 0

Views: 184

Answers (1)

user3151902
user3151902

Reputation: 3176

You are missing the AM/PM information:

formatter.dateFormat = "MM/dd/yyyy hh:mm:ss a"

(and you have to use lower case hh, as pointed out by vadian)

Upvotes: 1

Related Questions