Question

In my project i am getting "2008-02-01T10:03:23.793-06:00" this date format in string i have to convert into NSDate i am using

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 am print date value using NSLog i am getting value as null.

Please help me out

Thank You

Was it helpful?

Solution

NSDateFormatter won't be able to parse it, because the timezone doesn't conform with RFC 822 or GMT standard. The problem is on the last ":" to have it you need the "GMT".

The date that you have respects the RFC 3339.

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

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

OTHER TIPS

    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 the setLenient is needed.)

Just use that instead (note the "ZZZ" instead of "Z"):

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

I think you need to set a locale for the formatter too. Have a look here

Google's 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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top