JavaScript Date toLocaleDateString() Method

JavaScript Date toLocaleDateString() method is “used to convert a date to a string.” The new locales and options arguments let applications specify the language whose formatting conventions should be used and allow to customize the function’s behavior.

Syntax

dateObj.toLocaleDateString( [locales][, options])

Parameters

This method accepts two parameters as mentioned above and described below:

  1. locales: This parameter is an array of locale strings that contain one or more language or locale tags. Note that it is an optional parameter. If you want to use the specific language format in your application, specify that language in the locales argument.
  2. Options: It is also an optional parameter and contains properties that specify comparison options. Some properties are localeMatcher, timeZone, weekday, year, month, day, hour, minute, second, etc.

Return value

It is a string representing the date portion of the given Date instance according to language-specific conventions.

Example 1: Default locale

The toLocaleDateString() method is used without arguments so that it will use the browser’s default locale.

let date = new Date();

console.log(date.toLocaleDateString());

Output

8/16/2023

Example 2: Specifying a particular locale

let date = new Date();

// German format
console.log(date.toLocaleDateString('de-DE'));

// Japanese format
console.log(date.toLocaleDateString('ja-JP'));

Output

16.8.2023
2023/8/16

Example 3: Using options

You can also specify options to format the date in a specific way. Here, the date is formatted in a long format for US English.

let date = new Date();
let options = { year: 'numeric', month: 'long', day: 'numeric' };

console.log(date.toLocaleDateString('en-US', options));

Output

August 16, 2023

Browser Compatibility

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

That’s it!

Related posts

JavaScript Date toUTCString()

JavaScript Date toISOString()

JavaScript Date toTimeString()

Leave a Comment