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 .
source share