Does NSDateFormatter give different values ​​on device and simulator? What is work?

I am using NSDateFormatter, the problem is its consistency. If I use kCFDateFormatterMediumStyle, it gives the format "November 26, 2009" in the simulator, but on the device it gives "November 26, 2009."

Now I have a question: is this NSFormatter sure in the near future, or can Apple updates change the style again?

+5
source share
2 answers

NSDateFormatter formats the date for user preferences based on international settings on the device. Thus, you can never expect this string to be consistent due to the number of different ways that dates are displayed around the world.

I found a better way to format the date as a string (use to provide to web servers, etc.) to use NSDateComponents and convert them to strings.

+3
source

While the other answer mentioned above is technically correct, it does not give you the solution you are looking for. Although NSDateFormatter uses custom locales by default (and as such will not give you useful results), you can query a specific locale to get consistent results:

NSDateFormatter *frm = [[NSDateFormatter alloc] init];
[frm setDateFormat:@"EEE MMM dd HH:mm:ss ZZZ yyyy"];

NSLocale *en_US = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[frm setLocale:en_US];
[en_US release];
+17

All Articles