I am building an alarm clock app in Swift 2. I can't seem to find a decent way to find out whether the phone is set to 24 hours time or not. In addition I can't change the simulator to work with 24 hours so I can't even test some alternatives by playing with the time settings. Any ideas?
Tap Set. Tap Use 24- Hour Format to change the format to 24-hour time. Note that the time on the Notification bar reflects the change. You can return to 12-hour time by tapping Use 24-Hour Format again.
NSDateFormatter that fits this need. This specifier is j and you can get it through the dateFormatFromTemplate:options:locale: method of NSDateFormatter. The formatter returns h a if the locale is set to 12H, else HH if it's set to 24H, so you can easily check if the output string contains a or not. This relies on the locale property of the formatter that you should set to NSLocale.currentLocale() for getting the system's current locale:Swift 3
let locale = NSLocale.current let formatter : String = DateFormatter.dateFormat(fromTemplate: "j", options:0, locale:locale)! if formatter.contains("a") { //phone is set to 12 hours } else { //phone is set to 24 hours } Swift 2
let locale = NSLocale.currentLocale() let formatter : String = NSDateFormatter.dateFormatFromTemplate("j", options:0, locale:locale)! if formatter.containsString("a") { //phone is set to 12 hours } else { //phone is set to 24 hours } Reference: NSDateFormatter Class Reference - Unicode ts35-23 Date Field Symbol Table
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With