Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem converting Ruby on Rails DateTime to NSDate

Why is this ...

NSString *mydate = @"2011-07-20T23:59:00-07:00"

    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];

    NSLog(@"%@", [[dateFormatter dateFromString:mydate] description]);

... returning "(null)" in the console?

like image 273
wgpubs Avatar asked Aug 18 '11 23:08

wgpubs


1 Answers

Your date format of @"yyyy-MM-dd'T'HH:mm:ss'Z'" means that it's looking for a literal "Z" character at the end of the string. However, your string is:

@"2011-07-20T23:59:00-07:00"

I don't see a "Z" there, do you? Thus, the date comes back as nil because your string does not match the format specified.

According to the Date Formatting Patterns documentation, you're probably looking for the format string of:

@"yyyy-MM-dd'T'HH:mm:ssZZ"

However, even that might not work, because if you notice your source string has a colon (":") in between the hours and minutes of the timezone offset. There's no timezone specifier that accounts for that. If that is indeed the format in which ROR is returning the date, then ROR is wrong.

like image 65
Dave DeLong Avatar answered Oct 05 '22 06:10

Dave DeLong