I am trying to parse a date string from xml to an NSDate object in an iPhone application
I know that this must have been asked before, however, I believe that I have the correct syntax, but it does not work. Is there a problem with my code?
Date string I need to parse:
2011-01-21T12:26:47-05:00
The code I use to analyze 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 is greatly appreciated. Thank!
** Based on the link link from ChrisKent, I solved the problem as follows:
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]; }
ios objective-c iphone nsdate nsdateformatter
JohnRock Feb 15 2018-11-11T00: 00Z
source share