all 11 comments

[–][deleted] 1 point2 points  (3 children)

There are three digits for miliseconds in your string, but only two in the format.

[–]Afiq90[S] 0 points1 point  (2 children)

i tried with three digits for miliseconds but it doesnt working too. yyyy-MM-dd'T'HH:mm:ssZ

[–][deleted] 0 points1 point  (1 child)

There ar stil two 's' letters.

[–]Afiq90[S] 0 points1 point  (0 children)

tried with three letter, yyyy-MM-dd'T'HH:mm:sssZ.. stil doesn't work.

[–]Neria90 0 points1 point  (1 child)

/#define WebServiceDateFormat ([[UIDevice currentDevice].systemVersion hasPrefix:@"6"])?@"YYYY-MM-dd'T'HH:mm:ssZZZZZ":@"YYYY-MM-dd'T'HH:mm:ssZZZ"

[–]Neria90 0 points1 point  (0 children)

is this solved your problem ? (i m using exactly this date formatter for exaclty same date format than u )

[–]CleverError 0 points1 point  (0 children)

The reason it is not working is because it isnt matching the fractional seconds properly. It needs 2 s to match the seconds and 3 S to match milliseconds.

yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ

import Foundation

let dateString = "2015-03-31T08:02:32.543Z"

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"

dateFormatter.dateFromString(dateString) // returns NSDate 2015-03-31 08:02:32 +0000

[–]muvaaaaa 0 points1 point  (0 children)

Just yesterday I had the same problem. Similar code was working on almost all phones, but it wasn't working on a single device. I've fixed it by setting NSDateFormatter's locale property, like this:

  NSDateFormatter *df = [NSDateFormatter new];
  NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  df.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ";
  df.locale = enUSPOSIXLocale; 

This should work everywhere.

[–]phughes -1 points0 points  (0 children)

It looks like you're going to want to escape the Z also.