Reputation: 6337
My iOS app is going to display the current time. How do I determine whether the user's locale prefers 12-hour or 24-hour time formatting?
Upvotes: 5
Views: 2982
Reputation: 15400
You generally don't need to figure out what the user's locale settings are when displaying data to the user. If you use the relevant NSFormatter
(NSNumberFormatter
for numbers and NSDateFormatter
for date and time, which is the relevant one for you in this case), then you will automatically get the output that is appropriate to the user's locale... i.e what Apple calls the "localized representation of the object"
Upvotes: 0
Reputation: 5156
See the NSDateFormatter class http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
Something like:
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *dateString = [dateFormatter stringFromDate:date];
Upvotes: 1