5

I understood the javascript method toLocaleDateString() used computer settings.

Let's take the W3Schools example : when i change date and hour formats of my computer, the result is different in Firefox or IE (as expected), but Chrome still shows the same date format, why?

plucile
  • 306
  • 3
  • 4
  • 2
    [Let's not _"take the W3Schools example"_](http://www.w3fools.com). That site is full of inaccuracies. – Cerbrus Feb 12 '13 at 15:28
  • Duplicate: http://stackoverflow.com/questions/8002237/tolocaledatestring-not-working-in-firefox; lesser duplicate: http://stackoverflow.com/questions/14792949/date-tolocaledatestring-in-node – apsillers Feb 12 '13 at 15:39
  • If you are signed in a chrome Profile, then it takes the locale setting from your Signed in Gmail/Google Workspace account. – Waqar Ahmad May 01 '21 at 09:30

3 Answers3

2

From the MDN:

"The exact format depends on the platform, locale and user's settings."

And,

"You shouldn't use this method in contexts where you rely on a particular format or locale."

Basically, "Why" is because that's how Chrome does it. If you need a specific format, you're going to have to specify it yourself.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • Ok, thanks :) So, what should be the right way in js to display date differently for an english or en french user for example ? – plucile Feb 12 '13 at 15:50
  • 2
    The most reliable option would be to just get the date / time etc, and build a string like `year + '/' + month + '/' + day + ' ' + hour + ':' + minute + ':' + second;` – Cerbrus Feb 12 '13 at 15:57
1

From the EMCAScript 5 standard:

15.9.5.6 Date.prototype.toLocaleDateString ( )

This function returns a String value. The contents of the String are implementation-dependent, but are intended to represent the “date” portion of the Date in the current time zone in a convenient, human-readable form that corresponds to the conventions of the host environment’s current locale.

Chrome can represent the date as a locale date string in whatever manner it likes. The standard only supplies guidelines; it does not mandate a particular format. And, in fact, the result will vary not only between browsers but also within Chrome itself depending on your locale settings.

Community
  • 1
  • 1
apsillers
  • 112,806
  • 17
  • 235
  • 239
1

It looks like Chrome does not use the Windows regional settings, but its own settings instead. These are available via Settings > Advanced Settings > Language. However the date format is not explicitly defined, it is inferred from the language + country choice, for instance:

  • English (US) sets date format to mm/dd/yyyy
  • English (UK) sets date format to dd/mm/yyyy

(For anyone trying to change these, don't forget - like I did - to restart Chrome for the settings to take effect)

Back to the original question, it looks like it was legit to use toLocaleDateString() as long as the idea is to present the information in a format the human user understands. But this would be an ideal world, where every user has his/her browser properly configured. Instead, Chrome is set by default to English(US) as long as people leave it be in English, and it takes some googling (which most users won't do) to change these settings.

This makes it risky to use toLocaleDateString() even when not "relying on a particular format or locale". It looks like the only "serious" option for any cross-browser web application is to manage its own date format preferences (per user, of course...)

avat
  • 338
  • 3
  • 9