NSDateFormatter with regional format

I use this code to handle a date string coming from a json feed:

NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle: NSDateFormatterLongStyle]; [formatter setFormatterBehavior: NSDateFormatterBehavior10_4]; [formatter setDateFormat: @"EEE, dd MMM yyyy HH:mm:ss +0000"]; 

so if i call

 NSDate *date = [formatter dateFromString: @"Tue, 08 Sep 2009 19:21:27 +0000"]; 

I return a useful date if my regional format is the United States or the United Kingdom, but if I put it in Germany, it will return to zero. I understand that there are some differences in behavior in different locales, but if I define the format, should this not fix any inconsistencies?

+4
source share
2 answers

Names like "Tue" and "Sep" are English. Other languages ​​use different names.

If you want to analyze English dates regardless of the device region settings, set your DateFormatter language to en_US using the -setLocale: method.

+8
source

Thanks, fixed it: [formatter setLocale: [[[NSLocale alloc] initWithLocaleIdentifier: @ "US"] autorelease]];

+2
source

All Articles