![]() |
VOOZH | about |
The toLocaleDateString() method in JavaScript is used to convert the date and time of a Date object to a string representing the date portion using the locale-specific conventions.
dateObj.toLocaleDateString( [locales][, options])This method accepts two parameters as mentioned above and described below:
Note: The dateObj should be a valid Date object.
Example 1: Formatting Date with JavaScript's toLocaleDateString()
The code initializes a Date object and options for formatting. It then demonstrates the use of toLocaleDateString() to output the current date in different formats based on locale settings, with and without custom options.
3/14/2024 Thursday, Mar 14, 2024
Example 2: Without parameters return value of this method cannot be relied upon in scripting. It uses the operating system's locale conventions.
7/28/1993
Note: The locales and options arguments are not supported in all browsers. To check whether it is supported or not we can use the following function:
function toLocaleDateStringSupportsLocales() {
try {
new Date().toLocaleDateString('i');
}
catch (e) {
return e.name === 'RangeError';
}
return false;
}
We have a complete list of Javascript Date Objects, to check those please go through this article.
- Not specifying locales and options, leading to inconsistent formatting across different environments.
- Assuming a specific date format without verifying the locale settings.
- Overlooking the impact of user or system locale settings on the output.