Using SwiftUI, I want to determine 12 hour vs 24 hour from system settings

Using SwiftUI, I want to determine 12 hour vs 24 hour from system settings

Replies

ADDITIONAL INFORMATION


I’m using Xcode version 11.3.1 and Catalina version 10.15.1. These are not the latest versions but updating Xcode requires the latest version of Catalina and when I try to update Catalina it reports that my version is up to date. This is a separate issue that I have reported but no resolution at this time.


Notice the comments for the following code:


let date = Date(). // always returns time in 12 hour format

let calendar = Calendar.autoupdatingCurrent

let hour = calendar.component(.hour, from: date). // always returns time in 24 hour format


If I can read the current system settings for clock, I can adjust the hour to match the current system settings for clock.


I have read that the following code can be used to determine the clock setting.


let dateFormat = DateFormatter.dateFormat (fromTemplate: "j",options:0, locale: Locale.current)


However, dateFormat is always set to “j” regardless of the system settings for clock.


If anyone knows of a property that can be accessed for 12/24 hour in system setting I would appreciate knowing it.


Thanks

let dateFormat = DateFormatter.dateFormat (fromTemplate: "j",options:0, locale: Locale.current)


Should give you a string back, such as "HH" (for 24 hour time) or "h a" (for 12 hour time with the appropriate AM/PM distinction for the locale)

Thanks for the response. This did help and I can now move on to the next steps.

Best Regards,

James

This came up in another context, so I wanted to update this thread to point out that we now have a proper API for this, namely the hourCycle property within Foundation’s Locale. This is available in macOS 13 or later, and its aligned releases.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"