0

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

There are many proposed solutions at How can I determine if iPhone is set for 12 hour or 24 hour time display?

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'

  1. What is Uhr mean for? I guess it mean "clock" in German?
  2. 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?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • What is the purpose of knowing that? The framework can localize dates perfectly on your behalf. Yes, "Uhr" is clock in German. – vadian Feb 04 '23 at 07:58
  • Our app provides an UI option, for users to choose between 12/24-hours as their preference. When user is using the app for the first time, we need to set such UI option, to match with their system locale. – Cheok Yan Cheng Feb 04 '23 at 08:03
  • 2
    Why not just follow the system setting. There is no point in replicating a setting that already exists. Even if you take the initial value from the system, if the user changes the system setting your app won't follow. If you just use the system setting then the user always gets what they expect. – Paulw11 Feb 04 '23 at 08:07
  • This is a very common requirement for our app users, based on user feedback. For instance, for some users, even their system language is X, they prefer to use our app in Y language. Same goes to time format settings. – Cheok Yan Cheng Feb 04 '23 at 08:21
  • The system language is not necessarily directly related to the date and time settings. Maybe your users don't know that. For example I'm using English as system language but Swiss-German with custom settings as locale. – vadian Feb 04 '23 at 08:26
  • The way I would do it (well, I wouldn't...) is create a time for 11pm using date components and see why hour you get from a time formatter. 11 or 23 – Paulw11 Feb 04 '23 at 08:27
  • 4
    Or you can have 3 options, 12h, 24h or use system settings. That way you won’t need to know what user’s system settings are since you would either completely override them or strictly follow them – Joakim Danielson Feb 04 '23 at 09:54
  • try this answer [https://stackoverflow.com/a/69880682/11662833](https://stackoverflow.com/a/69880682/11662833) – Sreekuttan Feb 05 '23 at 06:29

1 Answers1

0

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")
}
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875