13

Situation: I want to use the JavaScript function Date.toLocaleDateString() to display a date in the users preferred locale. So far so well, but I want to display month and day with the 2-digit option.

As far as I know, you have to use Date.toLocaleDateString(locale, options) to display with options, but which value should I use for the locale option? Which variable does the toLocaleDateString() read internally to set the locale, so that I can read it out and pass it to the function call with 2 parameters?

2
  • You could try with undefined
    – Hacketo
    Commented Aug 7, 2015 at 8:58
  • 1
    From the MDN page referenced as containing the information on the locale parameter: "If the locales argument is not provided or is undefined, the runtime's default locale is used." Commented Aug 7, 2015 at 8:59

1 Answer 1

16

From the specification of toLocaleDateString:

  1. If locales is not provided, then let locales be undefined.

Implying you can set it to undefined yourself with no ill effects. This is backed up by the MDN reference documentation:

If the locales argument is not provided or is undefined, the runtime's default locale is used.

So you can call it with:

Date.toLocaleDateString(undefined, options);

to get the default locale as if you'd called it with no arguments.

2
  • Thank you, I simply didn't see the remark about undefined.
    – MilConDoin
    Commented Aug 7, 2015 at 9:10
  • @MilConDoin That info itself isn't on the MDN page for toLocaleDateString, but the page I linked to is linked behind the "Intl page" text under the "locales" parameter section. Commented Aug 7, 2015 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.