-1

I have tried and tried and researched and tried again but I cannot format this

The date is: "2013-08-08T11:10:09-07:00"

I've tried using "yyyy'-'MM'-'DD'T'HH':'mm':'ssZ" and a host of different permutations of this but to no avail. I think perhaps the server is sending the incorrect format of the timezone.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy'-'MM'-'DD'T'HH':'mm':'ssZ"];

return [dateFormatter dateFromString:[dictionary objectForKey:key]];

Any clues?

Thanks in advance

Nick Kirsten
  • 1,187
  • 11
  • 27
  • http://b2cloud.com.au/how-to-guides/nsdateformatter-format-strings Consult this, clue MM is wrong, DD is wrong. – Tim Oct 27 '13 at 16:40
  • Apologies, I have edited the post accordingly – Nick Kirsten Oct 27 '13 at 16:40
  • Also, you do not need the ' ' marks – Tim Oct 27 '13 at 16:41
  • Try - "yyyy-MM-dd'T'HH:mm:ssZZZ" – Tim Oct 27 '13 at 16:44
  • I have adjusted it to @"yyyy-M-ddTHH:mm:ssZ" and still no luck :/ – Nick Kirsten Oct 27 '13 at 16:46
  • I tried your solution Jeff and am still getting a nil date – Nick Kirsten Oct 27 '13 at 16:48
  • I can't answer my own question for 8 hours but my research has made me realise that the source data is incorrect. The timezone should not have a colon in it and should be -0700 instead of -07:00 – Nick Kirsten Oct 27 '13 at 16:54
  • The data you used in your example `2013-08-08T11:10:09-07:00` requires this format string to parse: `yyyy-MM-dd'T'HH:mm:ssZZZZZ`. You need 5 `Z` characters for the non-localized ISO GMT offset that your source date is using. – Jason Coco Oct 27 '13 at 16:59
  • awesome that worked! create the answer and I will mark it as correct. thanks! – Nick Kirsten Oct 27 '13 at 17:02
  • @Jeff Also, if your dates are coming from the server or something, escaping the time/date separators is not a bad idea because there is discussion of turning those into locale-replacing variables like letters currently, and I believe that the apple stuff may even do that in some places. So, unescaped, those `:` and `-` could suddenly change behavior in the future. – Jason Coco Oct 27 '13 at 17:04
  • Thanks Jason, good point! Will bear that in mind – Tim Oct 27 '13 at 17:36

1 Answers1

2

For the non-localized ISO offset that you're using—e.g., -07:00—you need to use 5 Z characters in your format. So, for the source data given, the correct format string would be:

yyyy-MM-dd'T'HH:mm:ssZZZZZ

While the currently used technical standard doesn't require date and time separators to be escaped, as Jeff mentions in the comments, it's probably not a bad idea to do so, especially if your source date is coming from a server or something. There are discussions on making those replacements, like the letters, that would change for locale-specific date and time separators. It also doesn't hurt to escape them from a technical perspective, it's just harder to read:

yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZZZ

For more information, the current date formatter is based on the Unicode Technical Spec #35. The date and time pattern specifications for #35 can be found at http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns

Jason Coco
  • 77,985
  • 20
  • 184
  • 180