Swift – Converting JSON date to Swift compatible date

I am trying to convert a date in which the javascript code is generating the current date using the Date() function. But when I print it out, I am getting nil.

my code:

        let date2 = data?[0] as! String

        var str = "2013-07-21T19:32:00Z"

        var dateFor: NSDateFormatter = NSDateFormatter()
        dateFor.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"

        var Date: NSDate? = dateFor.dateFromString(date2)


date2 => is printing out 2015-05-15T21:58:00.066Z
Date => is printing nil
str is just for testing and works perfectly.

Anyone see any flaws in the code?

77 thoughts on “Swift – Converting JSON date to Swift compatible date”