.toLocaleString()
In JavaScript, the .toLocaleString()
method formats a Date
object as a string according to the specified locale, considering cultural settings such as language
and date/time
formatting preferences specific to the chosen region or country.
Syntax
dateObj.toLocaleString(locales, options)
dateObj
: ADate
object representing the date and time to be formatted as a string based on the specified locale.locales
: Astring
or anarray of strings
that specifies one or morelocales
or language tags for formatting the date.options
: An object that allows customizing the formatting behavior, such as specifying the format fordate
,time
,numeric values
, and more.
Note: Both the
locales
andoptions
parameters in the.toLocaleString()
method are optional. If the parameters are not provided, the method will usedefault
values based on the runtime environment.
Example
In the example below, toLocaleString()
formats the current date
and time
according to the long date format with the full weekday
, month
, day
, and year
in English (United States) locale
:
const specificDate = new Date('2024-05-16');const locale = 'en-US';const options = {weekday: 'long',year: 'numeric',month: 'long',day: 'numeric',};console.log(specificDate.toLocaleDateString(locale, options));
The code above produces the following output:
Thursday, May 16, 2024
Codebyte Example
In the following example, .toLocaleString()
formats the current date and time with the full weekday
, year
, month
, day
, hour
, minute
, and second
in French (Morocco) locale (fr-MA):
All contributors
- Anonymous contributor
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