JavaScript Date.toLocaleString() Method

MD Niaz Rahman Khan Jan 30, 2023
  1. Syntax of JavaScript date.toLocaleString() Method
  2. Example Codes: Use the date.toLocaleString() Method
  3. Example Codes: Use the date.toLocaleString() Method With the Optional Parameter locales
  4. Example Codes: Use the date.toLocaleString() Method With the Optional Parameter options
JavaScript Date.toLocaleString() Method

In JavaScript, the Date object is used to represent Date and Time. We can declare this object with the new keyword.

This object provides different methods to customize the date and time. The date.toLocaleString() is one of them.

This method converts the Date object to date and time and represents it as a string.

Syntax of JavaScript date.toLocaleString() Method

date.toLocaleString(locales, options)

Parameters

locales (optional) locales refer to the countries’ languages that can be passed as an array of strings; this will format the date in a specific language. By default, this parameter uses the browser’s default language.
options (optional) It refers to an object that customizes the date and time format with different properties, including timezone, weekday, year, month, day, dayPeriod, hour, minute, second, and functionalSecondDigit. If these properties are not provided, the year, month, day, hour, minute, and second will be numeric.

Return

A string value of date and time is returned from the Date object based on the specific language conventions of the locale.

Example Codes: Use the date.toLocaleString() Method

const date = new Date();
let dateAndTimeString = date.toLocaleString()

console.log(dateAndTimeString);

Output:

9/7/2022, 12:11:36 AM

The Date object has been declared in the date variable. We get access to the toLocaleString() method from the Date object with a dot (.).

The date.toLocaleString() method returns the date and time as a string. To store the result, we declare a variable and print the variable to see the result in the output.

Example Codes: Use the date.toLocaleString() Method With the Optional Parameter locales

const date = new Date();
let USDateAndTimeString = date.toLocaleString('en-US')
let KoreanDateAndTimeString = date.toLocaleString('ko-KR')
let PersianDateAndTimeString = date.toLocaleString('fa-IR')

console.log(USDateAndTimeString)
console.log(KoreanDateAndTimeString)
console.log(PersianDateAndTimeString)

Output:

9/7/2022, 12:14:30 AM
2022. 9. 7. 오전 12:14:30
۱۴۰۱/۶/١٤، ۰:۱۴:۳۰

We provide different countries’ languages as the locales parameter to format our date and time as a string in that specified language. If we don’t specify any locales, it will print the date and time as a string based on the browser’s selected language.

Example Codes: Use the date.toLocaleString() Method With the Optional Parameter options

const date = new Date()
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
let dateStringWithOptions = date.toLocaleDateString('en-US', options)

console.log(dateStringWithOptions)

Output:

Wednesday, September 7, 2022, 06:17:53 PM

We define different options in the options object, which returns the customized date and time as a string in the output based on our provided options parameters. If we do not give any options, it will print the date and time string as numeric.

MD Niaz Rahman Khan avatar MD Niaz Rahman Khan avatar

Niaz is a professional full-stack developer as well as a thinker, problem-solver, and writer. He loves to share his experience with his writings.

LinkedIn

Related Article - JavaScript Date