1

I'm trying to convert a string to an NSDate, however the format always comes out as nil

The date I'm trying to convert is:

2012-08-16T16:20:52.619000+00:00

The date format I'm trying is:

@"yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZZ"

If I change the date to:

@"2012-08-16T16:20:52.619000+0000" // removing the : from +00:00

it works a treat, however I would

(I have also tried

@"yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZ:ZZ" 
@"yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZ':'ZZ" 

but that didn't work either).

Is it even possible to do this without doing string manipulation and removing the final ":"?

user200341
  • 253
  • 3
  • 13
  • 1
    Gee, we haven't had standard question #782 for some time now. (Use NSDateFormatter, but first you must scrub the string of that last ":" character.) – Hot Licks Aug 17 '12 at 18:48
  • possible duplicate of [Why NSDateFormatter can not parse date from ISO 8601 format](http://stackoverflow.com/questions/7925038/why-nsdateformatter-can-not-parse-date-from-iso-8601-format) @HotLicks – jscs Aug 17 '12 at 19:11
  • You are both wrong. I have provided an answer below and instead of modifying the date string, make use of "getObjectValue" – user200341 Aug 18 '12 at 00:04

3 Answers3

4

I did a final search around this and found out that you have to use

getObjectValue

rather than

dateFromString

In case someone else runs in to this issue, I post my method for converting such strings to NSDate

+ (NSDate *)dateFromString:(NSString *)dateString {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"];

    NSDate *theDate = nil;
    NSError *error = nil;
    [dateFormat getObjectValue:&theDate forString:dateString range:nil error:&error];
    [dateFormat release];
    return theDate;
}
user200341
  • 253
  • 3
  • 13
  • 1
    I don't see how getObjectValue would work any differently from dateFromString in this case. In either case it seems like the input string would need to be scrubbed. – Hot Licks Aug 17 '12 at 18:53
  • This doesn't change a thing. `dateFromString:` _calls through to_ `getObjectValue:...` The former is just a convenient wrapper for the latter, for when you don't care about the details of possible errors. – jscs Aug 19 '12 at 21:01
  • I suggest you try out the code before you mark it down or make such a statement. – user200341 Aug 20 '12 at 12:18
  • You still didn't test the code: here is more support for this http://stackoverflow.com/a/3968411/200341 – user200341 Aug 20 '12 at 18:26
  • I did try it. Did you try mine? They do _exactly the same thing_. – jscs Aug 21 '12 at 00:52
  • This is really interesting: I did try your code out and I added the results and code used to pastebin here: http://pastebin.com/S0wHERKc When I don't use the dateformatlogger it returns nil, however when I use the date format logger it returns the correct date. It's a shame this is being marked down in case anyone else has this issue, however I have found since then quite a few links to issues of the same nature (you just have to search for "rfc3339TimestampFormatterWithTimeZone") – user200341 Aug 21 '12 at 09:21
  • Limit still only SSS (three S), no micro seconds with getObjectValue. – malhal Dec 10 '14 at 22:34
1

It looks like you are using ISO 8601 formatted dates. If you are getting these from a web service, the format changes according to the format. Check this out:

http://boredzo.org/iso8601parser/

This will convert dates according to the format, and even when the format changes slightly.

J2theC
  • 4,412
  • 1
  • 12
  • 14
-1

How about something like

[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];

the Z has to be in single quotes.

Tom Fobear
  • 6,729
  • 7
  • 42
  • 74