更新时间:2023-02-26 16:33:13
我已经找到了一个不错的方法来确定这一点,我添加了一个小函数到 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