JavaScript | date.toLocaleString()
The date.toLocaleString() is an inbuilt function in JavaScript which is used to convert a date and time to a string.
Syntax:
dateObj.toLocaleString(locales, options)
dateObj should be a valid Date object.
-
Parameters:
- locales –This parameter is an array of locale strings that contain one or more language or locale tags. Note that it is an optional parameter. If you want to use a specific format of the language in your application then specify that language in the locales argument.
- Options –It is also an optional parameter and contains properties that specify comparison options.Some properties are localeMatcher, timeZone, weekday, year, month, day, hour, minute, second etc.
Return values: It returns date and time as a string value in the specific format that is specified by locale.
JavaScript code to show the working of date.toLocaleDateString() function:
Example 1: 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 return the result which is machine-specific.
<script> var d = new Date(); var result = d.toLocaleString(); document.write("date and time as a string = " + result); </script> |
Output:
date and time as a string = 6/26/2018, 10:28:17
Example 2:
This code prints the date and time in string format specified by locale parameter.
<script> var date = new Date(Date.UTC(2018, 5, 26, 7, 0, 0)); var options = { hour12: false }; document.write(date.toLocaleString("en-US")); document.write("<br>"); document.write(date.toLocaleString("en-US", options)); </script> |
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..
Supported Browsers: The browsers supported by JavaScript date.toLocaleString() 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 | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript Course | Objects in JavaScript
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Functions 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.




