JavaScript | date.toLocaleDateString()
The date.toLocaleDateString() is an inbuilt function in JavaScript which is used to convert a date to a string.
Syntax:
dateObj.toLocaleDateString( [locales][, options])
dateObj should be a valid Date object.
Parameters:
Return values: It returns a date as a string value in specific format that is specified by locale.
JavaScript code to show the working of date.toLocaleDateString() function:
Example 1:
This program prints the current date.
<script> var dateObj = new Date(); var options = { weekday: "long", year: "numeric", month: "short", day: "numeric" }; document.write(dateObj.toLocaleDateString("en-US")); document.write("<br>"); document.write(dateObj.toLocaleDateString("en-US", options)); </script> </html> |
Output:
6/24/2018 Sunday, Jun 24, 2018
Example 2:
Without parameters return value of this method cannot be relied upon in scripting.It uses the operating system’s locale’s conventions.
<script> var dateObj = new Date(1993, 6, 28, 14, 39, 7); document.write(dateObj.toLocaleDateString()); </script> |
Output:
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 following function :
function toLocaleDateStringSupportsLocales() { try { new Date().toLocaleDateString('i'); } catch (e) { return e.name === 'RangeError'; } return false; } |
Supported Browsers: The browsers supported by JavaScript date.toLocaleDateString() Function are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- JavaScript Course | Understanding Code Structure in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Operators in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript Course | Objects in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- How to include a JavaScript file in another JavaScript file ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




