I am trying to parse a date string from xml into an NSDate object in an iPhone app
I am aware this must have been asked before, however, I believe I have the right syntax, but it is not working. Is there a problem with my code?
The date string I need to parse is:
2011-01-21T12:26:47-05:00
The code I am using to parse it is:
self.dateFormatter = [[NSDateFormatter alloc] init];
[self.dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[self.dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[self.dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
...
else if([elementName isEqualToString:kUpdated]){
self.currentQuestion.updated = [self.dateFormatter dateFromString:self.currentParsedCharacterData ];
}
Any help greatly appreciated. Thanks!
**Based on the link reference from theChrisKent I fixed the problem like so:
else if([elementName isEqualToString:kLastOnDeck]){
NSString *dateStr = self.currentParsedCharacterData;
// we need to strip out the single colon
dateStr = [dateStr stringByReplacingOccurrencesOfString:@":"
withString:@""
options:0
range:NSMakeRange([dateStr length] - 5,5)];
self.currentQuestion.lastOnDeck = [dateFormatter dateFromString:dateStr];
}
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…