JavaScript Date toLocaleString()
Syntax & Examples


Date.toLocaleString() method

The toLocaleString() method returns a string with a locality-sensitive representation of this date. It overrides the Object.prototype.toLocaleString() method.


Syntax of Date.toLocaleString()

There are 3 variations for the syntax of Date.toLocaleString() method. They are:

1.
toLocaleString()

This method returns a string representing the date and time according to the default locale and options.

Returns value of type String.

2.
toLocaleString(locales)

Parameters

ParameterOptional/RequiredDescription
localesoptionalA string with a BCP 47 language tag, or an array of such strings, to specify the locale or locales.

This method returns a string representing the date and time according to the specified locale.

Returns value of type String.

3.
toLocaleString(locales, options)

Parameters

ParameterOptional/RequiredDescription
localesoptionalA string with a BCP 47 language tag, or an array of such strings, to specify the locale or locales.
optionsoptionalAn object with configuration options for date and time formatting, such as weekday, year, month, day, hour, minute, second, etc.

This method returns a string representing the date and time according to the specified locale and formatting options.

Returns value of type String.



✐ Examples

1 Using toLocaleString() method with no arguments

In JavaScript, we can use the toLocaleString() method to get the date and time portion of a date according to the default locale.

For example,

  1. Create a new Date object with the current date and time.
  2. Use the toLocaleString() method with no arguments.
  3. Log the resulting string to the console using console.log().

JavaScript Program

const date = new Date();
const localeString = date.toLocaleString();
console.log(localeString);

Output

Expected output: A string representing the date and time portion of the current date in the default locale format.

2 Using toLocaleString() method with a locale argument

In JavaScript, we can use the toLocaleString() method to get the date and time portion of a date in a specified locale.

For example,

  1. Create a new Date object with the current date and time.
  2. Use the toLocaleString() method with the locale argument 'de-DE' (German - Germany).
  3. Log the resulting string to the console using console.log().

JavaScript Program

const date = new Date();
const localeString = date.toLocaleString('de-DE');
console.log(localeString);

Output

Expected output: A string representing the date and time portion of the current date in 'de-DE' locale format (e.g., '31.5.2024, 15:45:30').

3 Using toLocaleString() method with locale and options arguments

In JavaScript, we can use the toLocaleString() method to get the date and time portion of a date in a specified locale with specific formatting options.

For example,

  1. Create a new Date object with the current date and time.
  2. Use the toLocaleString() method with the locale argument 'en-US' (English - United States) and options for weekday, year, month, day, hour, minute, and second.
  3. Log the resulting string to the console using console.log().

JavaScript Program

const date = new Date();
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' };
const localeString = date.toLocaleString('en-US', options);
console.log(localeString);

Output

Expected output: A string representing the date and time portion of the current date in 'en-US' locale format with specified options (e.g., 'Friday, May 31, 2024, 03:45:30 PM').

Summary

In this JavaScript tutorial, we learned about toLocaleString() method of Date: the syntax and few working examples with output and detailed explanation for each example.