Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a date string into an NSDate object in iOS?

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];  } 
like image 417
JohnRock Avatar asked Feb 15 '11 02:02

JohnRock


People also ask

How do I convert a date to a string in Swift?

let dateFormatter = DateFormatter() dateFormatter. locale = Locale(identifier: "en_US_POSIX") dateFormatter. dateFormat = "yyyy-MM-dd' 'HH:mm:ssZ" let date = dateFormatter. date(from:isoDate)!

How do I convert a date object to date?

Use the Date() constructor to convert a string to a Date object, e.g. const date = new Date('2022-09-24') . The Date() constructor takes a valid date string as a parameter and returns a Date object. Copied! We used the Date() constructor to convert a string to a Date object.

What is en_US_POSIX?

In most cases the best locale to choose is "en_US_POSIX", a locale that's specifically designed to yield US English results regardless of both user and system preferences.


1 Answers

You don't need near as many single quotes as you have (only needed on non date/time characters), so change this:

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

To this:

[self.dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"]; ... self.currentQuestion.updated = [self.dateFormatter dateFromString:[self.currentParsedCharacterData stringByReplacingOccurrencesOfString:@":" withString:@"" options:0 range:NSMakeRange([self.currentParsedCharacterData length] – 5,5)]]; 

Documentation here: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1

Unicode Format Patterns: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

Dealing with TimeZones with Colons (+00:00): http://petersteinberger.com/2010/05/nsdateformatter-and-0000-parsing/

like image 59
theChrisKent Avatar answered Sep 28 '22 23:09

theChrisKent