JavaScript Date toLocaleDateString()
Syntax & Examples
toLocaleDateString() method
The toLocaleDateString() method returns a string with a locality-sensitive representation of the date portion of this date based on system settings.
Syntax of toLocaleDateString()
There are 3 variations for the syntax of Date.toLocaleDateString() method. They are:
toLocaleDateString()
This method returns a string representing the date portion of this date according to the default locale and options.
Returns value of type String
.
toLocaleDateString(locales)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
locales | optional | A 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 portion of this date according to the specified locale.
Returns value of type String
.
toLocaleDateString(locales, options)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
locales | optional | A string with a BCP 47 language tag, or an array of such strings, to specify the locale or locales. |
options | optional | An object with configuration options for date formatting, such as weekday, year, month, day, etc. |
This method returns a string representing the date portion of this date according to the specified locale and formatting options.
Returns value of type String
.
✐ Examples
1 Using toLocaleDateString() method with no arguments
In JavaScript, we can use the toLocaleDateString() method to get the date portion of a date according to the default locale.
For example,
- Create a new Date object with the current date and time.
- Use the toLocaleDateString() method with no arguments.
- Log the resulting string to the console using console.log().
JavaScript Program
const date = new Date();
const localeDateString = date.toLocaleDateString();
console.log(localeDateString);
Output
Expected output: A string representing the date portion of the current date in the default locale format.
2 Using toLocaleDateString() method with a locale argument
In JavaScript, we can use the toLocaleDateString() method to get the date portion of a date in a specified locale.
For example,
- Create a new Date object with the current date and time.
- Use the toLocaleDateString() method with the locale argument 'en-GB' (English - United Kingdom).
- Log the resulting string to the console using console.log().
JavaScript Program
const date = new Date();
const localeDateString = date.toLocaleDateString('en-GB');
console.log(localeDateString);
Output
Expected output: A string representing the date portion of the current date in 'en-GB' locale format (e.g., '31/05/2024').
3 Using toLocaleDateString() method with locale and options arguments
In JavaScript, we can use the toLocaleDateString() method to get the date portion of a date in a specified locale with specific formatting options.
For example,
- Create a new Date object with the current date and time.
- Use the toLocaleDateString() method with the locale argument 'en-US' (English - United States) and options for weekday, year, month, and day.
- 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' };
const localeDateString = date.toLocaleDateString('en-US', options);
console.log(localeDateString);
Output
Expected output: A string representing the date portion of the current date in 'en-US' locale format with specified options (e.g., 'Friday, May 31, 2024').
Summary
In this JavaScript tutorial, we learned about toLocaleDateString() method of Date: the syntax and few working examples with output and detailed explanation for each example.