Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

time

ios

locale

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?

like image 690
Greg Maletic Avatar asked Nov 13 '22 14:11

Greg Maletic


1 Answers

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];
like image 52
railwayparade Avatar answered Nov 16 '22 02:11

railwayparade