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

发布于 2024-08-16 05:27:44 字数 146 浏览 7 评论 0原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日 2024-08-23 05:27:44

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

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
醉生梦死 2024-08-23 05:27:44

您使用的是 NSDateFormatter 类吗?据我所知,这尊重用户所设置的任何区域时间格式设置。

编辑-回复:您的评论:

格式字符串比较可能是正确的方法。大致如下:

 NSDateComponents *midnightComp = [[NSDateComponents alloc] init];
 [midnightComp setHour:0]
 [midnightComp setMinute:0];
 NSDateFormatter *format = [[NSDateFormatter alloc] init];
 [format setDateStyle:NSDateFormatterNoStyle];
 [format setTimeStyle:NSDateFormatterShortStyle];
 BOOL midnightIsZeros = [[[format stringFromDate:[[NSCalendar currentCalendar] dateFromComponents:midnightComp]] substringToIndex:2] isEqualToString:@"00"];

 [[NSUserDefaults standardUserDefaults] setBool:midnightIsZeros forKey:@"TimeWas24Hour"];

在应用程序退出时运行它,然后在应用程序启动时再次执行该操作,并根据默认值中存储的值进行检查。

Are you using the NSDateFormatter class? As far as I know, that respects whatever regional time-format settings the user has in place.

edit - re: your comment:

The format-string comparison might be the right approach. Something along the lines of:

 NSDateComponents *midnightComp = [[NSDateComponents alloc] init];
 [midnightComp setHour:0]
 [midnightComp setMinute:0];
 NSDateFormatter *format = [[NSDateFormatter alloc] init];
 [format setDateStyle:NSDateFormatterNoStyle];
 [format setTimeStyle:NSDateFormatterShortStyle];
 BOOL midnightIsZeros = [[[format stringFromDate:[[NSCalendar currentCalendar] dateFromComponents:midnightComp]] substringToIndex:2] isEqualToString:@"00"];

 [[NSUserDefaults standardUserDefaults] setBool:midnightIsZeros forKey:@"TimeWas24Hour"];

Run that as your app quits, then do it again when the app launches and check it against the value stored in the defaults.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文