Reputation: 612
Today, the 11th of September, 2017, JavaScript's toLocaleDateString()
method is outputting 9/11/2017
for me. But I am in the UK, so the formatting is wrong in this case. The MDN Web Docs tell me that this method returns "a formatted string in the default locale".
So, where/how is JavaScript detecting my default locale? Where is this set, or what does it depend on, and (how) can I change it?
Edited to add: I'm using Firefox 55.0.3 on Windows 10 if that makes any difference.
Upvotes: 31
Views: 25273
Reputation: 275
I would just like to answer this in 2023 for anyone struggling with exactly the issue of the original post: where Date
gets its default locale. Most Google results will land you on navigator.languages
, but my machine was definitely not using those values. Here's what I saw on my console:
new Date().toDateString()
// 'Fri Aug 04 2023'
navigator.languages
// (2) ['en-US', 'en']
new Date().toLocaleDateString()
// '04/08/2023'
new Date().toLocaleDateString(navigator.languages[0])
// '8/4/2023'
Though my languages are US-only, my machine region is set to Ireland, and toLocaleDateString
was reading that somehow and formatting the date in the Irish style, DD/MM/YYYY, as opposed to the US style, MM/DD/YYYY. After 30+ minutes of searches and drudging through lacking documentation, I finally found this post, which led me to my answer.
At least in Chrome on MacOS, the Date
object sources its locale from Intl.DateTimeFormat().resolvedOptions().locale
, which was not documented anywhere that I could find. Hopefully this can save someone the frustration that I just went through.
Upvotes: 12
Reputation: 78
I checked JavaScript date and time formatting in Chrome, Firefox, and Edge on my Windows 10 PC, with interesting results. My language tag should be "en-NZ', and I thought the locale behavior would follow accordingly.
Of the three browsers, only Edge had picked up the correct language tag by default, presumably from the OS, while Chrome and Firefox both defaulted to en-US. Edge also defaults to using the correct locale settings.
It seems I can influence the locale in Chrome by changing language in Advanced Settings to en-NZ, but the default locale behavior I get is as for en-GB. Firefox doesn't seem to care what my preferred language is for locale behavior, which depends only on the language "used to display menus, messages, and notifications from Firefox". The only English language options for that are US, Canada, or UK.
In Chrome and Firefox the only way to ensure the correct locale behavior seems to be to explicitly specify it in the JavaScript code.
Upvotes: 5
Reputation: 25310
To summarize shortly, detecting the current locale is implementation dependent and may differ from environment to environment. Your default language may also depend on the installer you've used to install your browser.
The not so short version:
Following the ECMAScript spec, conforming browsers (and other environments, such as Node.js) should implement localization following the ECMAScript Internationalization API (ECMA-402), which only outlines the following for getting the default locale:
The DefaultLocale abstract operation returns a String value representing the [...] language tag for the host environment’s current locale.
This means that getting the default locale is implementation dependent and can differ from browser to browser. This is intentional, as it allows browser vendors to keep their current, differing implementations to stay conforming without much fuss.
While it's true that it would be nice to have this standardized as well, it's more beneficial to get everyone on board for a broad spec first and then work out the little kinks later.
Most modern browsers allow you to change your current default locale in their preferences (Chrome shown):
Upvotes: 4
Reputation: 102
You can pass locale as parameter. See the example below:
var date = new Date().toLocaleDateString("en-GB");
Upvotes: -3