.toLocaleDateString()
The .toLocalDateString()
date method returns a modified string of a given Date
object, usually for events. It is translated to a specific language according to an event’s locale
and other options
.
Syntax
const myEventDate = new Date('December 31, 2021');const locale = 'en-US';const options = {weekday: 'long',month: 'long',day: 'numeric',year: 'numeric',};myEventDate.toLocaleDateString(locale, options);
Example Without Parameters
When used without any parameters, .toLocaleDateString()
returns a string with the month
, day
, and year
options defaulted to numeric
. How they’re arranged and formatted depends on the default locale
in which the method was used.
In the example below, .toLocaleDateString()
is being used in the en-US
locale (more specifically, in the America/Los_Angeles
timezone).
const date = new Date(2021, 12, 31);console.log(date.toLocaleDateString());// Output: 12/31/2021
Format Based on Language/Region
Dates with a specific language format can be returned by passing a country or region’s short-code (string) as the locale
parameter, as shown in the example below:
const date = new Date(2021, 11, 31);// US English - month/day/yearconsole.log(date.toLocaleDateString('en-US'));// Korean - year. month. day.console.log(date.toLocaleDateString('ko-KR'));
The output will be:
12/31/20212021. 12. 31.
Format with Options
The options
parameter can be used to provide additional formatting to the returned date string:
const birthday = new Date(2022, 3, 11);options = {year: 'numeric',month: 'long',day: 'numeric',weekday: 'long',};console.log(birthday.toLocaleDateString('en-US', options));
The output will be:
Monday April 11, 2022
Codebyte Example
In the following example the variables region
and options
can be modified, to print the Date in a custom format:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours