Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone SDK NSString To NSDate

I got a string from parsing a XML file which looks like this: Fri, 09 Apr 2010 00:00:45 +0200 and the corresponding pattern should be this "EEE, dd MMM yyyy HH:mm:ss ZZ", but I get (null).

This is my code:

NSString *dateString = @"Fri, 09 Apr 2010 00:00:45 +0200";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"];

NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"date:%@",date); // result date:(null)

Edit:

This works for me now, I had to switch to en-US locale:

NSLocale* usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-US"];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setLocale:usLocale];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"];

NSDate *date = [dateFormatter dateFromString:dateString];
like image 965
scud Avatar asked Apr 11 '10 21:04

scud


1 Answers

Your code worked for me when I changed this line:

[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"];

to this line:

[dateFormatter setDateFormat:@"EEE',' dd MMM yyyy HH:mm:ss ZZ"];
like image 107
eipxen Avatar answered Sep 29 '22 14:09

eipxen