navigator object. Mozilla-based browsers such as Firefox store it in the
language property, and Internet Explorer uses the
browserLanguage. Here's a short script that displays the browser language. You can add it to any web page to see what language your browser is running under.
Here is what came up in my browsers:
This value does not match my PC's default locale, which is set to English Canada (
en_CA). That's because the browser locale is part and parcel of the particular browser build that you installed. For that reason, you have to be sure that you install a browser that's built for your particular locale. Otherwise, you could run into some issues.
toLocaleDateString(): Returns the date portion of a Date object as a string, using locale conventions
toLocaleTimeString(): Returns the time portion of a Date object as a string, using locale conventions
toLocaleString(): Returns both the date and time portions of a Date object as a string, using locale conventions
The following script shows the results of the three methods above when applied to today's date:
The above code outputs:
Date.parse() with a value of "Apr 1, 2007" yields the correct result of
1175400000000 in a US locale browser. Conversely, trying to parse the same date string in the German formatting of "1. April 2007" returns the "
NaN" ("Not a Number") global constant. That is not a good thing because you won't be able to work with that date at all. The only consolation here is that you can use the
isNaN() method to test the validity of your date string against the browser's locale and react accordingly: