![]() |
VOOZH | about |
The date.toLocaleString()method is used to convert a date and time to a string using the locale settings.
dateObj.toLocaleString(locales, options)This method accepts two parameters as mentioned above and described below:
Note: The dateObjshould be a valid Date object.
Example 1: Below is the example of the Date toLocaleString()method.
Output:
Date and Time of apocalypse: 26/10/2020, 12:30:00Example 2:This code prints the current date and time. Also, In this code toLocaleString()method does not use any parameter so it uses the operating system’s locale’s conventions and returns a result that is machine-specific.
Output:
date and time as a string = 1/9/2023, 1:17:10 PMExample 3:This code prints the date and time in string format specified by the locale parameter.
Output:
6/26/2018, 12:30:00 PM
6/26/2018, 12:30:00
Note: The toLocaleString()method is different from toLocaleDateString()as toLocaleDateString()converts only the date of a Date object into a string but toLocaleString()converts date and time to a string.
We have a complete list of Javascript Date methods, to check those please go through the Javascript Date Object Complete Reference article.
The browsers supported by JavaScript Date toLocaleString() Methodare listed below: