I just simply can’t figure out how to set NSDateFormatter date format so [formatter dateWithString:] does not return a nil.
The date looks as follows :
2008-08-18T14:32:22.1875000+01:00
I tried
formatter setDateFormat:@"yyyy-MM-ddTHH:mm:ss.fffffffzz+GMT"];
Doesn’t seem to work :(. Any suggestions, please help.
Ok thanks for answering, it seems like I mislead people with my question a little so I try to rephrase it, sorry for that, maybe its just bit late for programming. I need a date wrapper object so I don’t have to parse the string containing the date myself. Basically I get a string from a Database I need to parse to I can use it in my UI.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZ";
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSDate *date = [formatter dateFromString:@"2008-08-18T14:32:22.1875000+01:00"];
NSLog(@"%@", date);
That prints nil
What I want is NSString->NSDat, so I can ask it maybe what month it contains, or what time it contains. I see now that NSDate does not have such method. That was a foolish thing to assume. Any Ideas,now?
Per the unicode documentation there’s no f. Assuming you want subseconds, that should be S. You also need to put the T in quotes if you want it to transfer literally. I think you’re confused about z too, and probably want ZZZZ rather than zz+GMT. So:
Per the QA1480, you may also want explicitly to set a locale of
en_US_POSIX, if you want to produce a technical string for storage. It reserves the right to adjust your requested fields otherwise per the current locale, assuming you want to display the string. So that’d be:As per ctrahey’s answer, that doesn’t quite get the format you want, ending up at e.g.
i.e. the letters ‘GMT’ have crept in there. Switching to a single Z gets rid of the explicit reference to GMT but also removes the colon.
Edit: per your example code:
That fails because your input string doesn’t match the supplied date format — it’s missing the letters GMT before the +01:00. Since there doesn’t seem to be a unicode date string that exactly matches what you have in your database, probably the easiest thing is just to remove the colons. E.g.
For me that outputs
2008-08-18 13:32:22 +0000.