iphone - 检测iPhone是否以12小时或24小时模式显示时间?

标签 iphone localization regional

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

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

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

最佳答案

我已经找到了一种确定这一点的好方法,方法是在 NSLocale 类别中添加一个小函数。它看起来相当准确,并且在多个区域进行测试时没有发现任何问题。

@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 - 检测iPhone是否以12小时或24小时模式显示时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1972108/

相关文章:

iphone - 将 UIView 添加到 UICollectionviewcell

iphone - iphone 的数据存储在哪里?

postgresql - 如何在 Postgres 上对挪威文本进行排序

c# - 在 TimeZoneInfo 对象中本地化显示名称以显示不同文化中的时区

datetime - 一个更好的 VB6 CDate

iphone - 核心位置 didEnterRegion 无效

iphone - WWDC 2010 年 session 401/402 : Is the Quest application source available for download?

iphone - Localized.strings 有时可以工作

ruby-on-rails - 从网络检测区域设置(列表分隔符)

c# - 如何更改 Windows 用户的区域设置/日期格式?