JavaScript Date.toLocaleDateString() Method
-
Syntax of JavaScript
date.toLocaleDateString()
Method -
Example Codes: Use the
date.toLocaleDateString()
Method -
Example Codes: Use the
date.toLocaleDateString()
Method With the Optional Parameterlocales
-
Example Codes: Use the
date.toLocaleDateString()
Method With the Optional Parameteroptions
In JavaScript, the Date
object is used to represent the 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.toLocaleDateString()
is one of these methods used to convert the Date
object to date and represents it as the string.
Syntax of JavaScript date.toLocaleDateString()
Method
date.toLocaleDateString(locales, options)
Parameters
locales (optional) |
This refers 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 uses the browser’s default language. |
options (optional) |
It refers to an object that customizes the date format with different properties. Some properties are timezone, weekday, year, month, and day. |
Return
A string value is returned from the Date
object based on the specific language conventions of the locale.
Example Codes: Use the date.toLocaleDateString()
Method
const date = new Date();
let dateString = date.toLocaleDateString()
console.log(dateString);
Output:
9/2/2022
The Date
object has been declared in the date
variable. We get access to the toLocaleDateString()
method from the Date
object with a dot.
This method returns the date 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.toLocaleDateString()
Method With the Optional Parameter locales
const date = new Date();
let USDateString = date.toLocaleDateString('en-US')
let KoreanDateString = date.toLocaleDateString('ko-KR')
let PersianDateString = date.toLocaleDateString('fa-IR')
console.log(USDateString)
console.log(KoreanDateString)
console.log(PersianDateString)
Output:
9/2/2022
2022. 9. 2.
۱۴۰۱/۶/۱۱
We provided different countries’ languages as the locales
parameter to format our date string in that specified language. This parameter is optional; if we do not specify locales
, it will print the date string based on the browser’s selected language.
Example Codes: Use the date.toLocaleDateString()
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:
Friday, September 2, 2022
We defined different options in the options
object, which returns the customized date strings as the output based on our provided options
parameters. If we do not give any options, it will print the date string as numeric.
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