If you are working with user-visible dates, you should avoid setting the date format string, because it is very difficult to predict what your format string will look like in all possible user configurations. Rather, you should try to limit yourself to date and time style settings (via - [NSDateFormatter setDateStyle:] and - [NSDateFormatter setTimeStyle:]).
On the other hand, if you are working with fixed-format dates, you must first set the date formatting locale to something suitable for your fixed-format. In most cases, the best choice is "en_US_POSIX", a language that is specifically designed to produce results in English, regardless of user and system preferences. "en_US_POSIX" is also time-invariant (if the US at some point in the future changes the format of dates, "en_US" will change to reflect the new behavior, but "en_US_POSIX" will not) between machines ("en_US_POSIX" also works on iPhone OS, as on Mac OS X, as well as on other platforms).
Once you have set "en_US_POSIX" as the language format for date formatting, you can set the date format string, and the date format will behave consistently for all users.
The above information and more can be found at Apple Technical Q & A QA1480
Here is a snippet of code from my application that implements the above recommendation:
static NSDateFormatter* dateFormatter = nil; if (!dateFormatter) { dateFormatter = [[NSDateFormatter alloc] init]; NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]; assert(enUSPOSIXLocale != nil); [dateFormatter setLocale:enUSPOSIXLocale]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; dateFormatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss +0000"; }
memmons
source share