NSDateFormatter Detects 24-Hour Clock in OS X and iOS

I would like to check if the user has selected 12 hour or 24 hour time as preference in OS X and iOS. Therefore, I would like to determine if the user has completed the following:

  • On a Mac, the System preference indicates the date and time, use 24-hour time
  • On the iPhone, preference is set in the settings, in general, in the date and time, for 24 hours.

I currently have the following code, but it always returns the time represented by the 12-hour clock, even if the system settings set by the user are for the 24-hour time.

let timeFormatter = NSDateFormatter() timeFormatter.locale = NSLocale.currentLocale() timeFormatter.dateStyle = NSDateFormatterStyle.NoStyle timeFormatter.timeStyle = NSDateFormatterStyle.ShortStyle let ampmtext = timeFormatter.stringFromDate(NSDate()) println(ampmtext) if ampmtext.rangeOfString("M") != nil { println("12-hour clock") } else { println("24-hour clock") } 

I would like to find a solution written in Objective-C and Swift for Mac and iPhone that can determine if a device’s clock shows 24-hour or 12-hour time.

+6
source share
3 answers

The date template function has a neat trick. There is a template specifier j that will turn into a clock format depending on whether the locale is used for 12 or 24 hours. It will turn into something like ha within 12 hours (en_US in this case) or HH for the 24-hour format (en_GB).

Then you just need to check if the date format contains a

 //let locale = NSLocale(localeIdentifier: "de_DE") //let locale = NSLocale(localeIdentifier: "en_US") //let locale = NSLocale(localeIdentifier: "en_GB") let locale = NSLocale.currentLocale() let dateFormat = NSDateFormatter.dateFormatFromTemplate("j", options: 0, locale: locale)! if dateFormat.rangeOfString("a") != nil { println("12 hour") } else { println("24 hour") } 

This should also take into account the format.

This is similar to your check, but you should not try to check AM or PM . These are English versions, there are many more. For example, in Germany, if you use the 12-hour format, iOS uses nachm. and vorm. . The correct way is to check the format for a .

+16
source

Swift 4

Here is a quick 4 interpretation of the accepted answer:

 func is24Hour() -> Bool { let dateFormat = DateFormatter.dateFormat(fromTemplate: "j", options: 0, locale: Locale.current)! return dateFormat.index( of: "a") == nil } 

Using:

 if is24Hour() { // should show 24 hour time }else{ // should show 12 hour time } 
+2
source

The date format used in the menu bar on Mac OS X is contained in the file ~/Library/Preferences/com.apple.menuextra.clock.plist . On my system (en_US locale), this looks like after hours:

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>DateFormat</key> <string>EEE h:mm a</string> <key>FlashDateSeparators</key> <false/> <key>IsAnalog</key> <false/> </dict> </plist> 

and so on for a 24-hour time:

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>DateFormat</key> <string>EEE H:mm</string> <key>FlashDateSeparators</key> <false/> <key>IsAnalog</key> <false/> </dict> </plist> 

You can get the format string using defaults read com.apple.menuextra.clock DateFormat from the command line or NSUserDefaults from Obj-C or Swift. Then you can check if the format string contains a . If the clock is not set to a 24-hour time.

+1
source

Source: https://habr.com/ru/post/981586/


All Articles