Search code examples
iphonelocalizationregional

Detect whether iPhone is displaying time in 12-Hour or 24-Hour Mode?


How can you detect whether iPhone is displaying time in 12-Hour or 24-Hour Mode?

I currently monitor the current region for changes, however that's not the only setting that affects how times are displayed. The 24-hour toggle in the date & time settings overrides the current regions settings.

Is there any way to detect this 12/14 hour setting?


Solution

  • I've figured out a decent way of determining this with a little function I've added to a category of NSLocale. It appears to be pretty accurate and haven't found any problems with it while testing with several regions.

    @implementation NSLocale (Misc)
    - (BOOL)timeIs24HourFormat {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [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 is24Hour = amRange.location == NSNotFound && pmRange.location == NSNotFound;
        [formatter release];
        return is24Hour;
    }
    @end