xcode 检测 iPhone 24 小时制时间设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12214398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:23:07  来源:igfitidea点击:

Detect iPhone 24-Hour time setting

xcodecocoa-touchuidatepicker

提问by Darren

I'm using a UIDatePicker to select a time. I am also customising the background of the picker, however I need 2 different images depending on whether the user is using 12 hour mode (which displays AM/PM column) or 24 hour mode. How can I detect the users setting for the 12/24 hour time?

我正在使用 UIDatePicker 来选择时间。我也在自定义选择器的背景,但是我需要 2 个不同的图像,具体取决于用户是使用 12 小时模式(显示 AM/PM 列)还是 24 小时模式。如何检测 12/24 小时制的用户设置?

Thanks

谢谢

采纳答案by patryk

and there's probably many, many more...

可能还有很多很多……

回答by Dave DeLong

Even shorter than the others:

甚至比其他人更短:

NSString *format = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
BOOL is24Hour = ([format rangeOfString:@"a"].location == NSNotFound);

Explanation

解释

The string formatting character to represent the am/pm symbol is "a", as documented in Unicode Locale Markup Language – Part 4: Dates.

表示 am/pm 符号的字符串格式化字符是“a”,如Unicode Locale Markup Language – Part 4: Dates 中所述

The same document also explains the special template symbol "j":

同一份文件还解释了特殊的模板符号“j”:

This is a special-purpose symbol. It must not occur in pattern or skeleton data. Instead, it is reserved for use in skeletons passed to APIs doing flexible date pattern generation. In such a context, it requests the preferred hour format for the locale (h, H, K, or k), as determined by whether h, H, K, or k is used in the standard short time format for the locale. In the implementation of such an API, 'j' must be replaced by h, H, K, or k before beginning a match against availableFormats data. Note that use of 'j' in a skeleton passed to an API is the only way to have a skeleton request a locale's preferred time cycle type (12-hour or 24-hour).

这是一个特殊用途的符号。它不得出现在图案或骨架数据中。相反,它保留用于传递给执行灵活日期模式生成的 API 的骨架。在这样的上下文中,它请求区域设置的首选小时格式(h、H、K 或 k),这取决于在区域设置的标准短时间格式中是否使用 h、H、K 或 k。在此类 API 的实现中,在开始与 availableFormats 数据匹配之前,'j' 必须替换为 h、H、K 或 k。请注意,在传递给 API 的骨架中使用“j”是让骨架请求区域设置的首选时间周期类型(12 小时或 24 小时)的唯一方法。

The NSStringmethod dateFormatFromTemplate:options:locale:is described in Apple's NSDateFormatterdocumentation:

Apple 的文档中描述了该NSString方法:dateFormatFromTemplate:options:locale:NSDateFormatter

Returns a localized date format string representing the given date format components arranged appropriately for the specified locale.

返回一个本地化的日期格式字符串,表示为指定的语言环境适当排列的给定日期格式组件。

So, what that method will do is turn the @"j"you pass in as a template in to a format string suitable for NSDateFormatter. If this string contains the am / pm symbol @"a"in it anywhere, then you know the locale (and other user settings being interrogated by the OS for you) wants am / pm to be displayed.

因此,该方法将做的是将@"j"您作为模板传入的格式字符串转换为适合NSDateFormatter. 如果此字符串@"a"在任何地方包含 am / pm 符号,那么您就知道区域设置(以及操作系统为您询问的其他用户设置)希望显示 am / pm。

回答by CryingHippo

Swift (3.x) version of two most popular solutions in form of Date extension :

两个最流行的解决方案的 Swift (3.x) 版本,以 Date 扩展的形式:

extension Date {

    static var is24HoursFormat_1 : Bool  {
        let dateString = Date.localFormatter.string(from: Date())

        if dateString.contains(Date.localFormatter.amSymbol) || dateString.contains(Date.localFormatter.pmSymbol) {
            return false
        }

        return true
    }

    static var is24HoursFormat_2 : Bool {
        let format = DateFormatter.dateFormat(fromTemplate: "j", options: 0, locale: Locale.autoupdatingCurrent)
        return !format!.contains("a")
    }

    private static let localFormatter : DateFormatter = {
        let formatter = DateFormatter()

        formatter.locale    = Locale.autoupdatingCurrent
        formatter.timeStyle = .short
        formatter.dateStyle = .none

        return formatter
    }()
}

Usage :

用法 :

Date.is24HoursFormat_1
Date.is24HoursFormat_2

Swift (2.0) version of two most popular solutions in form of NSDate extension :

NSDate 扩展形式的两个最流行的解决方案的 Swift (2.0) 版本:

extension NSDate {

    class var is24HoursFormat_1 : Bool  {
        let dateString = NSDate.localFormatter.stringFromDate(NSDate())

        if dateString.containsString(NSDate.localFormatter.AMSymbol) || dateString.containsString(NSDate.localFormatter.PMSymbol) {
            return false
        }

        return true
    }

    class var is24HoursFormat_2 : Bool {
        let format = NSDateFormatter.dateFormatFromTemplate("j", options: 0, locale: NSLocale.autoupdatingCurrentLocale())
        return !format!.containsString("a")
    }

    private static let localFormatter : NSDateFormatter = {
        let formatter = NSDateFormatter()

        formatter.locale    = NSLocale.autoupdatingCurrentLocale()
        formatter.timeStyle = .ShortStyle
        formatter.dateStyle = .NoStyle

        return formatter
    }()
}

Please note that Apple says following on NSDateFormatter (Date Formatters) :

请注意,Apple 在 NSDateFormatter ( Date Formatters)上说如下:

Creating a date formatter is not a cheap operation. If you are likely to use a formatter frequently, it is typically more efficient to cache a single instance than to create and dispose of multiple instances. One approach is to use a static variable.

创建日期格式化程序并不是一项廉价的操作。如果您可能经常使用格式化程序,则缓存单个实例通常比创建和处置多个实例更有效。一种方法是使用静态变量。

Thats the reason for static let

这就是静态 let 的原因

Secondly you should use NSLocale.autoupdatingCurrentLocale() ( for is24HoursFormat_1 ), that way you will always get the actual current state.

其次,您应该使用 NSLocale.autoupdatingCurrentLocale() (用于 is24HoursFormat_1 ),这样您将始终获得实际的当前状态。