Hapeki Hapeki - 5 months ago 15
Swift Question

Swift: Converting String to NSDate returns nil

I have the following date:

2016-08-09 03:00:00.0


I want to convert it from a
String
to a
NSDate
like so:

var stringDate = "2016-08-09 03:00:00.0"

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd' 'HH:mm:ss"
let date = dateFormatter.dateFromString(stringDate)


However the converted date turns out to be nil. What am I doing wrong here?

Answer

Try this as the format:

yyyy-MM-dd HH:mm:ss.S