Reputation: 171
I use an instance of NSDateFormatter to help display a date string in my app, but after I change Region Format in Settings in simulator from "United States" to "United Kingdom", the example in settings show date as "5, January 2014", which proves change succeeded. But when I open my app in the simulator, the date is still "Jan 5, 2014". I used following code to check the locale in my app:
NSLocale *locale = [NSLocale currentLocale];
NSString *currentLocale = [locale objectForKey:NSLocaleIdentifier];
NSLog(@"Current locale is %@", currentLocale);
And it logs: Current locale is en_US. It seems that my app doesn't get the current locale in simulator setting. It's supposed to be I change the locale in simulator setting, and my app can reflect the change. Is there anything wrong? The date format code is
static NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = NSDateFormatterMediumStyle;
dateFormatter.timeStyle = NSDateFormatterNoStyle;
self.dateLabel.text = [dateFormatter stringFromDate:item.dateCreated];
I am using iOS Simulator version 8.1, Xcode version 6.1.1, iOS 8.1, SDK 8.1
The dateFormatter code is put in the viewWillAppear method of one view controller, after region format changed, and create a new view controller still presents the old locale. Same result even kill the app. I register for locale change notifications in a table view and refresh it. It can reflect the locale change. Couldn't understand the difference.
Upvotes: 3
Views: 2772
Reputation: 1465
I have the same problem. It looks like a bug in the simulator: https://devforums.apple.com/message/1063365
Upvotes: 1
Reputation: 8066
You have to set the locale of your NSDateFormatter
NSLocale *locale = [NSLocale currentLocale];
NSString *currentLocale = [locale objectForKey:NSLocaleIdentifier];
NSLog(@"Current locale is %@", currentLocale);
static NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:locale]; // <- Here
dateFormatter.dateStyle = NSDateFormatterMediumStyle;
dateFormatter.timeStyle = NSDateFormatterNoStyle;
self.dateLabel.text = [dateFormatter stringFromDate:item.dateCreated];
Upvotes: 0