3

I really need your help guys. The following piece of code works fine in a Swift Playgound and with any iOS8 or 8.1 simulator. But with iOS7 and 7.1, the NSDate object is always set to nil.

The object dateString contains a JSON string (ISO 8601 format) like 2015-02-28T20:15:00+0100 I'm trying to convert this date string into a NSDate object with the following code :

let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone.localTimeZone()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.ZZZ"
if let dateString = (json as NSDictionary).valueForKey("dateAndTime") as? String
{
   let dateObject = dateFormatter.dateFromString(dateString)
}

Where's my mistake ? I'm getting confused! Many thanks

Roccapina
  • 87
  • 1
  • 7

1 Answers1

5

The problem is with your time zone offset, your date format is not proper for that. Use the following format,

"yyyy-MM-dd'T'HH:mm:ssZZZZZ"
Sandeep
  • 20,908
  • 7
  • 66
  • 106
  • Amazing. Many thanks. I was getting crazy. By the way, I still don't understand why ut worked fine with iOS8. – Roccapina Mar 01 '15 at 10:05
  • Y should only be used for WeekOfYear. – Leo Dabus Mar 01 '15 at 11:04
  • I got the answer thanks. The problem was effectively with the "...zzz" format I was using. "...ZZZZZ" works fine. With the "...zzz" format, dateFromString always gave nil. Thanks guys for your help. – Roccapina Mar 01 '15 at 11:52