iOS: check whether the device time format is set to 24 or 12 hours


A quick tip that might be useful anytime you manage time in different timezones and countries with different system settings. Ran asked on SOF if there’s a way to detect the device time mode set by user (12 or 24 hours).

There is a template specifier for NSDateFormatter that fits this need. This specifier is j and you can get it through the dateFormatFromTemplate:options:locale: method of NSDateFormatter. The formatter returns h a if the locale is set to 12H, else HH if it’s set to 24H, so you can easily check if the output string contains a or not.

This relies on the locale property of the formatter that you should set to NSLocale.currentLocale() for getting the system’s current locale:

Remember that in the simulator, you don’t have the 12/24 hours toggle in the settings like it happens on the real devices. A trick for changing this is by editing the date style of the Simulator, without affecting the currently selected region or calendar. You can find this option in Settings > General > Language and Region > Advanced.


📝 License: CC BY-SA 3.0 with attribution requiredLink to this post.
📷 Back to the Future, © 1985 Universal Pictures


Nicola Giancecchi

Born in '93 in a tiny state lost in the middle of the italian peninsula (hint: it's not the Vatican City). Great computer science enthusiast and Apple fan, I love to write code in Swift, but I also like to work with other languages and environments (PHP, Android, Windows, etc). Three times Apple WWDC Scholarship Recipient. Coding since 2008, developing iOS apps since 2012.


Leave a Reply

Your email address will not be published. Required fields are marked *