Reputation: 176
My computer is defined to use 24h format:
However, when I use var date = new Date("10/25/2020 19:32")
and then date.toLocaleTimeString()
Upvotes: 1
Views: 1549
Reputation: 147403
When no options are provided, the output of toLocaleString is entirely implementation dependent so may or may not use host settings, however they are almost certain to treat it as equivalent to a language of "default" with no options, i.e.
new Date().toLocaleString('default') === new Date().toLocaleString()
which returns true in every browser I tested. The important part is:
[toLocaleString is] intended to represent the Date in the current time zone in a convenient, human-readable form that corresponds to the conventions of the host environment's current locale.
Note that it should use the conventions of the locale (i.e. place or region), not the host system.
When a language and/or options are provided, the output is governed by ECMA-402 and is primarily based on the language code provided as the first argument (misnamed "locale"). Options also affect the output, however the host settings are not used other than to obtain the language code to use in the case of "default" for the first argument.
The underlying principle of the toLocale methods is that appropriate formatting can be determined from a language and other fairly vague options. Implementation developers then work out what format to use for each language and option combination. Clearly the host system settings can't be used when language or options are provided as that defeats the entire purpose of the function.
Upvotes: 2
Reputation: 16688
Your assumption that the browser will use the settings in the operating system is probably wrong.
You leave it to the browser to choose a locale for you.
My guess is that it is based on window.navigator.languages
. This could be "en-US" in your case, hence the 12h format.
Upvotes: 0