Search code examples
javascriptlocalizationbrowserlocale

How does JavaScript "Date" object determine the locale of the user's browser?


I am trying to understand how the JavaScript running in the various browsers determines the locale that will be used by the JavaScript Date object when using the method toLocaleString().

I have changed the language and locale settings of my system through Windows control panel and I have also changed every browser's language settings to the same. The function still returned an English date.

Is there a registry setting on windows that you have to manually set? Did I miss some setting somewhere? Or do I have to get a Windows install disc for particular regions?


Solution

  • A great article from one of my favourite blogs on JScript's behavior:

    I believe we are now in "always default to US-English" mode in IE. It's confusing because the script engines have the ability to change the locale used for the error messages independent of the locale used to format dates, numbers, etc.

    and

    Date.prototype.toLocaleDateString, toLocaleTimeString, and toLocaleString are complicated by some bizarre weirdnesses in the Win32 NLS API. To work around various problems, only dates between 1600 and 10000 AD are localized. Hebrew date formats for years after 2240 AD are also not supported. Once we jump through those hurdles, the Win32 APIs GetDateFormat and GetTimeFormat are used to format the strings. (I'm vaguely recalling that there was also a bug in there involving the Thai calendar but I don't remember the details.)

    https://blogs.msdn.com/ericlippert/archive/2004/05/18/jscript-localization-and-those-wacky-newfoundlanders.aspx

    And from the MDC (Mozilla Developer Center):

    The toLocaleString method relies on the underlying operating system in formatting dates. It converts the date to a string using the formatting convention of the operating system where the script is running. For example, in the United States, the month appears before the date (04/15/98), whereas in Germany the date appears before the month (15.04.98). If the operating system is not year-2000 compliant and does not use the full year for years before 1900 or over 2000, toLocaleString returns a string that is not year-2000 compliant. toLocaleString behaves similarly to toString when converting a year that the operating system does not properly format.