Get time cycle type 12-hour or 24-hour

suggest change

Checking whether the current date contains the symbol for AM or PM

Objective-C

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);

This checks the formatted current time. A 12-hour locale can be reported as 24-hour when that particular string omits the AM/PM symbol. The template below asks the locale which cycle it prefers.

Requesting the time cycle type from NSDateFormatter

Objective-C

NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
NSRange containsA = [formatStringForHours rangeOfString:@"a"];
BOOL is24h = containsA.location == NSNotFound;

This uses a special date template string called “j” which according to the ICU Spec …

[…] requests the preferred hour format for the locale (h, H, K, or k), as determined by the preferred attribute of the hours element in supplemental data. […] Note that use of ‘j’ in a skeleton passed to an API is the only way to have a skeleton request a locale’s preferred time cycle type (12-hour or 24-hour).

That last sentence is important. It “is the only way to have a skeleton request a locale’s preferred time cycle type”. Since NSDateFormatter and NSCalendar are built on the ICU library, the same holds true here.

Reference

The second option was derived from this answer.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents