0

I am calling an API to get some data. API response returns date and time in String format but when I parse it into NSDate using DateFormatter it shows nil instead of date.String formate that I get from API is 2016-10-09T12:15:17.000Z.

Here is my code.

let dateString = dataDict.objectForKey("schedule_date") as! String
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
print(dateFormatter.dateFromString(dateString))
Nirav D
  • 71,513
  • 12
  • 161
  • 183
Syed Qamar Abbas
  • 3,637
  • 1
  • 28
  • 52

2 Answers2

3

change your date formatter like yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

let dateString = dataDict.objectForKey("schedule_date") as! String
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
print(dateFormatter.dateFromString(dateString))

Note: SSS is here for milliseconds.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
0

nil only comes when the format of the date set in the date formatter is not according to the date string received. Check your date format for the value received.

Abhishek Arora
  • 395
  • 2
  • 12