Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDateFormatter with region format

I use this code to process a date string coming in 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 get back a usable date if my region format is United States or United Kingdoms, but if I set it to Germany it returns nil. I understand there are some differences in behaviors across different locales, but if I define a format shouldn't that correct for any inconsistencies?

like image 773
user170385 Avatar asked Dec 29 '22 15:12

user170385


2 Answers

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

If you want to be able to parse English dates independent of the device's region settings, set your DateFormatter's locale to en_US using the -setLocale: method.

like image 151
Darren Avatar answered Jan 09 '23 17:01

Darren


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

like image 31
user170385 Avatar answered Jan 09 '23 18:01

user170385