Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Date.toLocaleDateString() - set only options

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?

like image 948
MilConDoin Avatar asked Aug 07 '15 08:08

MilConDoin


1 Answers

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.

like image 59
James Thorpe Avatar answered Nov 07 '22 23:11

James Thorpe