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?
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.
Thanks fixed it up with: [formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"US"] autorelease]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With