How to format numbers and dates based on user locale settings?

I need a way to automatically format Date and Number objects based on my users locale settings.

So far I have used the function toLocaleString()for dates. For numbers is also available toLocaleString(), but as you can see in jsFiddle , I got the results are very different between browsers. With English (US) on my Windows machine, I get the following:

  • IE9: 15,000.00
  • Firefox: 15,000
  • Chrome: 15000

In Chrome, it toLocaleString()doesn't seem to work for numbers at all. Besides this approach, I also tried:

  • To use the MicrosoftAjax.js library localeFormat(), but no matter what language standard I set on my PC (using the Region and Language dialog), dates and numbers were still generated in the US format.
  • Use libraries of type Globalize . Although they offer formatting capabilities, they cannot automatically determine the user's language.

So, to summarize: how to automatically format numbers and dates to comply with the regional settings of a user viewing a web page in a way that works in all major browsers (IE, Firefox, Chrome)?

+5
source share
2 answers

.toLocaleString() Javascript , .

API- ECMAScript i18n (, , , ) - , , , , .

- , Javascript. , Accept-Language IMHO , . - , , .

+8

API @Jon . Number(123456).toLocaleString(), Chrome "123,456", ( ). IE11/Edge, caniuse.

0

All Articles