1

I've read through the various posts on SO about this, but have still not solved the problem.

Here's the string that I'm trying to parse into an NSDate object:

2012-04-08T12:00:00.00+02:00

I understand that the problem is the time zone format. Here's the date format string that I'm trying:

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

Thanks for any help.

edc1591
  • 10,146
  • 6
  • 40
  • 63
  • 1
    This looks a lot like: http://stackoverflow.com/questions/2201216/is-there-a-simple-way-of-converting-an-iso8601-timestamp-to-a-formatted-nsdate which has three answers. – DRVic Apr 07 '12 at 19:26
  • 2
    I think that thread doesn't address this issue -- there's another thread somewhere that does. And the gist of the answer is that you must somehow remove that last ":" character before attempting to parse the date with NSDateFormatter. – Hot Licks Apr 07 '12 at 19:48
  • 1
    I've seen that question. It doesn't answer my question. Answer 1 doesn't even answer the question, answer 2 doesn't use `NSDateFormatter`, and answer 3 uses methods that don't exist on iOS. – edc1591 Apr 07 '12 at 22:19
  • 1
    For those who don't know how to do this `yyyy-MM-dd'T'HH:mm:ssZZZZZ` – JOA80 Sep 03 '13 at 10:40
  • 1
    For the string `2013-09-01T14:23:12.2177735+00:00` the following formatter worked for me: `yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ` – Luke Sep 19 '13 at 08:35

2 Answers2

3

Three things:

  • Use the -[NSDateFormatter getObjectValue:forString:range:error:] method to learn how much of the string was parsed and what error prevented parsing further.

  • The ZZZZ format string corresponds to something like "GMT+02:00", with that "GMT" in there. You may need to inject that into the string you're parsing for it to work.

  • Apple's Data Formatting Guide advises "Consider Unix Functions for Fixed-Format, Unlocalized Dates".

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Thanks! Injecting the "GMT" did the trick. I'll also note that a helpful tip is to log out what `NSDateFormatter` expects a date using your format to look like, like this: `[dateFormatter stringFromDate:[NSDate date]]`. – edc1591 Apr 07 '12 at 22:41
0

Now the dedicated NSISO8601DateFormatter is available.

MacMark
  • 6,239
  • 2
  • 36
  • 41