Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if time format is in 12hr or 24hr format

Is there any way to detect if the current device of the app uses 12h our 24h format, so that I can use one NSDateFormatter for 12h and one for 24h depending on the users language/loaction setting? Just Like the UIDatePicker detects and shows the AM/PM picker if it is 12h format.

like image 228
JonasG Avatar asked Sep 16 '11 17:09

JonasG


1 Answers

I figured it out, its pretty easy. I just added this code to viewDidLoad :

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setLocale:[NSLocale currentLocale]]; [formatter setDateStyle:NSDateFormatterNoStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; NSString *dateString = [formatter stringFromDate:[NSDate date]]; NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]]; NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]]; BOOL is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound); [formatter release]; NSLog(@"%@\n",(is24h ? @"YES" : @"NO")); 

And it perfectly returns YES or NO depending on the locale.

like image 115
JonasG Avatar answered Sep 24 '22 10:09

JonasG