Hapeki Hapeki - 1 year ago 63
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
to a
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 Source

Try this as the format:

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