How do you use the toLocaleDateString() method in a Date object in JavaScript?

The toLocaleDateString() method in JavaScript is used to convert the date and time value of a Date object into a human-readable string in the format that is appropriate for the current locale. This method is part of the built-in Date object in JavaScript and can be used to format dates according to the conventions of the user’s language and region.

In this article, we will explore the usage of the toLocaleDateString() method and provide code snippets to demonstrate its functionality.

Syntax:

The syntax of the toLocaleDateString() method is as follows:

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

This method takes two optional parameters:

  1. locales: A string or an array of strings that specifies the language or region codes to use for formatting the date. If no value is specified, the default locale of the browser is used.

  2. options: An object that contains additional options for formatting the date. This parameter is optional.

Example Usage:

Here is an example of how to use the toLocaleDateString() method to format a Date object:

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

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

In this example, we create a new Date object and assign it to the variable date. We then create an options object that specifies the format for the date string we want to create. This options object specifies that we want the full name of the weekday, the full name of the month, and the numeric value of the year and day.

Finally, we call the toLocaleDateString() method on the date object and pass in the options object as the second parameter. We also specify the locale as ‘en-US’, which means we want the date to be formatted according to the conventions of the English language in the United States.

The output of this code will be a string representing the current date in the format “Monday, May 10, 2023”.

Specifying Locales:

The toLocaleDateString() method supports the use of locales to format dates according to the conventions of different languages and regions. The locale parameter can be a string or an array of strings that specifies the language or region codes to use for formatting the date.

Here is an example of how to use the locale parameter to format a date in Spanish:

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

console.log(date.toLocaleDateString("es-ES", options));

Output:

miércoles, 10 de mayo de 2023

In this example, we use the locale 'es-ES', which means we want the date to be formatted according to the conventions of the Spanish language in Spain. The output of this code will be a string representing the current date in Spanish, formatted according to the options specified in the options object.

Specifying Options:

The options parameter in the toLocaleDateString() method allows you to specify additional options for formatting the date string. This parameter is an object that can contain any of the following properties:

Here is an example of how to use the options parameter to format a date in a specific way:

let date = new Date();
let options = { year: "2-digit", month: "numeric", day: "numeric" };

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

Output:

5/10/23

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: