Date format for 2008-02-01T10: 03: 23.793-06: 00

In my project, I get "2008-02-01T10: 03: 23.793-06: 00" this date format in a string that I have to convert to NSDate, I use

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"]; NSDate* dateFromString = [dateFormatter dateFromString:@"2008-02-01T10:03:23.793-06:00"]; 

But when I print the date value using NSLog, I get the value as null.

Please help me

thanks

+4
source share
5 answers

NSDateFormatter will not be able to parse it because the time zone does not match RFC 822 or GMT by default . The problem is the latter: "you need GMT for this.

The date you respect RFC 3339 .

You will need to use NSFormatter to analyze it. You should use getObjectValue:forString:errorDescription:

Set the line @"yyyy-MM-dd'T'HH:mm:ss.SSSZ" .

-3
source
  NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"]; [formatter setLenient:YES]; NSString* buggeredTime = [[sourceDate substringToIndex:26] stringByAppendingString:[sourceDate substringFromIndex:27]]; NSDate* dueDate = [formatter dateFromString:buggeredTime]; [formatter setDateFormat:@"'Time Due: 'hh:mm a MM/dd/yy"]; NSString* dateString = [formatter stringFromDate:dueDate]; [formatter release]; 

(Dunno offhand if setLenient is required.)

+6
source

Just use this instead (note the “ZZZ” instead of the “Z”):

 [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"]; 
+2
source

I think you need to set the locale for formatting. Look here

+1
source

Google GData Objective-C Client has a class (GDataDateTime) that can parse RFC 3339 dates.

http://code.google.com/p/gdata-objectivec-client/source/browse/trunk/Source/Elements/GDataDateTime.m

+1
source

All Articles