Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 807
Matt Smith Avatar asked Feb 20 '10 10:02

Matt Smith


People also ask

How do I find the locale of my browser?

To get the user's locale in the browser, access the first element of the languages property on the navigator object, e.g. navigator. languages[0] . The property returns an array of strings that represent the user's preferred languages.

How does JavaScript date work?

A JavaScript date is fundamentally specified as the number of milliseconds that have elapsed since the ECMAScript epoch, which is defined as January 1, 1970, UTC (equivalent to the UNIX epoch).

How do I find my locale date?

Use the toLocaleString() method to get a date and time in the user's locale format, e.g. date. toLocaleString() . The toLocaleString method returns a string representing the given date according to language-specific conventions. Copied!

What is locale in JavaScript?

Locale (Runtime - JavaScript) A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user.


1 Answers

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.

like image 144
Andy E Avatar answered Sep 21 '22 17:09

Andy E