Is it possible to detect if user's machine is using 12 hour clock (am/pm) or 24 hour clock (military time)?
One way would be to check users locales, but then it is just massive list of locale comparison and someone from U.S who wants 12 hour clock can send me just en locale, not US_en and I have no way of knowing her preferences. At the same time someone from U.S might be set her machine to use 12 hour time format and doesn't want 12 hour clock.
EDIT:
date.toLocaleTimeString();
Would work it theory, as user Mouser suggested below, but unfortunately it's bugged on WebKit browsers (tested on Chrome and new Opera on Windows) and for some reason always returns am/pm time.
Example: http://jsfiddle.net/sjuaL3p4/
So I guess I have to rephrase my question if anyone has an idea how to accomplish it on webkit browsers also.
Besides the fact that this should never be done,
.toLocaleTimeString()
wouldn't be a good solution even if it did work across browsers. It returns a time string following the format set by the user's computer, but it's possible to have a 24-hour clock that still shows AM/PM, as well as a 12-hour clock that doesn't. It's not feasible to accurately or reliably detect this.This simple line seems to be working for me.
Though it still doesn't work in Chrome (works in IE and Firefox)
This solution works in most browsers, but bugs in Chrome.
Workaround Chrome; A proof of concept
This is an ugly work-around for Chrome. It sniffs out the users
country_code
viareverse geolocation
. That code is checked against an array with countries using the 12 hour system. This solution is ugly because you need user permission to get geolocation data and if the users locale is different it will give the wrong information, but will provide you with the country of the user. I strongly advise against using this example. It's purely for inspiration purposes. I made it up as a proof of concept.toLocaleTimeString
should give the time in a format that reflects the user's preferences but it is unreliable.I'm on a Debian system. The output of
locale
is:Here are a few experiments with
date
:The
%X
format tellsdate
to output the time according to the locale. The results above are exactly as expected. SettingLC_TIME
is a way to change only the time format but keep everything else in the locale intact. So someone in the US could use a 24h time format even if the default foren_US
is 12.I've tried Mouser's script on my system:
So far so good. However,
I get the same results with Chrome. It seems that both Firefox and Chrome ignore
LC_TIME
.