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

后端 未结 6 1347
执笔经年
执笔经年 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 05:00

    And here is a Swift 3.0 updated version

    func using12hClockFormat() -> Bool {
    
        let formatter = DateFormatter()
        formatter.locale = Locale.current
        formatter.dateStyle = .none
        formatter.timeStyle = .short
    
        let dateString = formatter.string(from: Date())
        let amRange = dateString.range(of: formatter.amSymbol)
        let pmRange = dateString.range(of: formatter.pmSymbol)
    
        return !(pmRange == nil && amRange == nil)
    }
    

提交回复
热议问题