What is a reliable way, to check user system locale is using 12-hours or 24-hours format?

We would like to know, whether a user system locale is using 12-hours or 24-hours format?

There are many proposed solutions at https://stackoverflow.com/q/1929958/72437

One of the solutions are

let formatString = DateFormatter.dateFormat(fromTemplate: "j", options: 0, locale: Locale.current)!
let hasAMPM = formatString.contains("a")

However, to me, this is not a correct solution.

When I tested with de_DE (German is using 24-hours), the returned string is HH 'Uhr'

What is Uhr mean for? I guess it mean "clock" in German?

There are so many other locales and one of them might contain letter a.

Does anyone know, what is a correct way, to check whether user system locale is using 12-hours or 24-hours format?

Accepted Reply

I have discovered the following solution. It works great both for Taiwan and Germany as per testing. Hopefully, it works fine for other locales as well. If not, please let me know the mistake. Thank you.

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short

let amSymbol: String? = dateFormatter.amSymbol
let pmSymbol: String? = dateFormatter.pmSymbol

guard let amSymbol = amSymbol, let pmSymbol = pmSymbol else {
    print("24-hour format")
}

let dateAsString = dateFormatter.string(from: Date())

if dateAsString.contains(amSymbol) || dateAsString.contains(pmSymbol) {
    print("12-hour format")
} else {
    print("24-hour format")
}
  • You've marked as correct before testing… So don't expect many more answers now. Or post a new thread. I may have a solution to propose.

  • Hi, I find it behaves correctly after testing for Taiwan and Germany. Do you find a use case where it is not correct?

Add a Comment

Replies

May be not the best, but I would do as follows:

  • in a var, get the date formatted
  • add 12 hours
  • get the date formatted again
  • Compare
  • If hours are different, it is 24 format. If the same, it is 12 format.

I have discovered the following solution. It works great both for Taiwan and Germany as per testing. Hopefully, it works fine for other locales as well. If not, please let me know the mistake. Thank you.

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short

let amSymbol = dateFormatter.amSymbol
let pmSymbol = dateFormatter.pmSymbol

if amSymbol != "" || pmSymbol != "" {
    print("12-hour format")
} else {
    print("24-hour format")
}
  • Sorry. Please ignore this answer as it is incorrect.

Add a Comment

I have discovered the following solution. It works great both for Taiwan and Germany as per testing. Hopefully, it works fine for other locales as well. If not, please let me know the mistake. Thank you.

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateStyle = .none
dateFormatter.timeStyle = .short

let amSymbol: String? = dateFormatter.amSymbol
let pmSymbol: String? = dateFormatter.pmSymbol

guard let amSymbol = amSymbol, let pmSymbol = pmSymbol else {
    print("24-hour format")
}

let dateAsString = dateFormatter.string(from: Date())

if dateAsString.contains(amSymbol) || dateAsString.contains(pmSymbol) {
    print("12-hour format")
} else {
    print("24-hour format")
}
  • You've marked as correct before testing… So don't expect many more answers now. Or post a new thread. I may have a solution to propose.

  • Hi, I find it behaves correctly after testing for Taiwan and Germany. Do you find a use case where it is not correct?

Add a Comment