Greg Maletic
Greg Maletic

Reputation: 6337

iOS: Display time in 24-hour format if locale appropriate?

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

Answers (2)

Clafou
Clafou

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

railwayparade
railwayparade

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

Related Questions