"2015-06-30T17:30:36.000Z" is an example of DateTime value in MySQL. How to convert it to NSDate in Swift?
Below is my code but it does not work
var sqlDateFormatter = NSDateFormatter()
sqlDateFormatter.dateFormat = "yyyy’-‘MM’-'dd’T'HH’:'mm’:'ss'Z’"
self.whenPublished = self.sqlDateFormatter.dateFromString("2015-06-30T17:30:36.000Z")
You just need to add the milliseconds thats it is missing at the end of your dateFormat string. Try like this:
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let string = "2015-06-30T17:30:36.000Z"
if let date = dateFormatter.date(from: string) {
print(date) // "2015-06-30 17:30:36 +0000"
}
Note: You need to use regular single cote '
instead of curved cote ’