Using NSDateFormatter for RSS-style date

For one of my first Iphone apps, i needed to convert a RSS-style date string, like "Sat, 08 May 2010 11:36:56 +0200", to a more short format, and in my own language (spanish). And that was the question. The formatting is easy:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString: rssdatetoformat];
dateFormat.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"es_ES"] autorelease]; [dateFormat setDateFormat:@"EEEE, d MMMM yyyy"];
NSString *dateString = [dateFormat stringFromDate:date];
[dateFormat release];

We create the formatter, we give it a format and a locale, but it is wrong, it always ignores the locale and give us an English formatted date. What i'm missing? When creating the date from the RSS string, the formatter not only needs to know what is the format of the string ("EEE, dd MMM yyyy HH:mm:ss Z"), it also needs to know that the string is in the "en_US" locale. With this tip, the RSS date string is formatted right to a Spanish shorter date (or to any other non-english locale) :

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
dateFormat.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
[dateFormat setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString: rssdatetoformat];
dateFormat.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"es_ES"] autorelease];
[dateFormat setDateFormat:@"EEEE, d MMMM yyyy"];
NSString *dateString = [dateFormat stringFromDate:date];
[dateFormat release];

Leave a comment
I have read and accept the Privacy Policy

The comments sent by each user will always be visible in the corresponding post, and will not be used for any other purpose. The user has the right to access, rectify and suppress said comments, as reflected in our Privacy Policy