Detect if time format is in 12hr or 24hr format

后端 未结 6 1377
执笔经年
执笔经年 2020-12-08 04:18

Is there any way to detect if the current device of the app uses 12h our 24h format, so that I can use one NSDateFormatter for 12h and one for 24h depending on the users lan

6条回答
  •  渐次进展
    2020-12-08 04:36

    Here is the Swift version:

    func using12hClockFormat() -> Bool {
    
        let formatter = NSDateFormatter()
        formatter.locale = NSLocale.currentLocale()
        formatter.dateStyle = NSDateFormatterStyle.NoStyle
        formatter.timeStyle = NSDateFormatterStyle.ShortStyle
    
        let dateString = formatter.stringFromDate(NSDate())
        let amRange = dateString.rangeOfString(formatter.AMSymbol)
        let pmRange = dateString.rangeOfString(formatter.PMSymbol)
    
        return !(pmRange == nil && amRange == nil)
    }
    

提交回复
热议问题