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

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]; } 
+48
ios objective-c iphone nsdate nsdateformatter
Feb 15 2018-11-11T00:
source share
7 answers

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

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

For 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 templates: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

Working with time zones with columns (+00: 00): http://petersteinberger.com/2010/05/nsdateformatter-and-0000-parsing/

+64
Feb 15 '11 at 2:41
source share

I had the same colon problems at the end. Here is the function I used to normalize the date to make NSDate happy.

 /** * Timezones are returned to us in the format +nn:nn * The date formatter currently does not support IS 8601 dates, so * we convert timezone from the format "+07:30" to "+0730" (removing the colon) which * can then be parsed properly. */ - (NSString *)applyTimezoneFixForDate:(NSString *)date { NSRange colonRange = [date rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@":"] options:NSBackwardsSearch]; return [date stringByReplacingCharactersInRange:colonRange withString:@""]; } 
+3
Feb 15 2018-11-11T00:
source share

It took me a while to find a simple answer for this. and there are many solutions related to attracting additional third-party code.

For those who are struggling with this now and only support iOS 6 and above.

You can set the date format for

 [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"] 

And this will correctly handle "-05: 00" with a colon.

+3
Jun 25 '15 at 19:32
source share

Decisions must be changed:

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

To these designations:

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

Hope this helps you.

+1
Dec 30 '11 at 11:36
source share

ISO8601DateFormatter

I have had great success in this library, available as CocoaPod.

https://github.com/boredzo/iso-8601-date-formatter

Using

 - (ISO8601DateFormatter *)dateFormatter { static ISO8601DateFormatter *dateFormatter = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ dateFormatter = [[ISO8601DateFormatter alloc] init]; dateFormatter.includeTime = YES; }); return dateFormatter; } NSDate *date = [[self dateFormatter] dateFromString:@"2015-05-09T13:06:00"]; 
+1
May 9 '15 at
source share

The problem is the ā€œZā€ at the end. As you write it in quotation marks, your parser expects the literal character Z in your timeline, but it is not. You want the format character Z without quotes to indicate that your time string contains time zone information. What it does is -05: 00 at the end of your line is the time zone.

Since you are expecting time zone information, setting the time zone in your date formatting is pretty pointless. And check out the link for Unicode formatting templates, this link contains the final information that you should trust, first of all, the answers you get here.

+1
May 09 '15 at 12:24
source share

Starting with iOS 10, the system provides NSISO8601DateFormatter for this particular format.

0
Sep 05 '17 at 21:06 on
source share



All Articles