When coding a website and formatting a date, I want to use the locale that the user has set in their browser.
So for example, if a user has customised their chrome://settings/languages
setting in Chrome to something non-default, that's the value I want to use.
However, when I run the code below in the console of such a browser, I get two different values.
[window.navigator.language, new Intl.DateTimeFormat().resolvedOptions().locale]
// Array [ "en-AU", "en-US" ]
navigator.language
gives me the expected value, but new Intl.DateTimeFormat().resolvedOptions().locale
does not.
MDN for both items seems (to me) to indicate that they should be returning the same value:
- NavigatorLanguage.language
returns a string representing the preferred language of the user
- Intl.DateTimeFormat locale parameter
To use the browser's default locale, omit this argument or pass undefined.
Should both of these calls be returning the locale the user configured in their browser settings?
en-US
, but my region is set tode
and my time formatting is German as well, yetIntl.DateTimeFormat().resolvedOptions().locale
will returnen-US
when it should returnen-DE
, actually. – Disassemble