检测 iPhone 是以 12 小时还是 24 小时模式显示时间? [英] Detect whether iPhone is displaying time in 12-Hour or 24-Hour Mode?

查看:22
本文介绍了检测 iPhone 是以 12 小时还是 24 小时模式显示时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测 iPhone 是以 12 小时还是 24 小时模式显示时间?

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

我目前监控当前区域的变化,但这并不是影响时间显示方式的唯一设置.日期和日期中的 24 小时切换时间设置会覆盖当前区域设置.

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.

有什么方法可以检测到这个 12/14 小时设置吗?

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

推荐答案

我已经找到了一个不错的方法来确定这一点,我添加了一个小函数到 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

这篇关于检测 iPhone 是以 12 小时还是 24 小时模式显示时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆