且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检测iPhone是否在12小时或24小时模式下显示时间?

更新时间:2023-02-26 16:33:19

我已经找到了一个很好的方法来确定这一点,我已经添加了一个 NSLocale 的类别。它似乎相当准确,并且在测试多个地区时没有发现任何问题。

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