JavaScript | date.toLocaleTimeString()
The date.toLocaleTimeString() is an inbuilt function in JavaScript which is used to fetch the time from a given Date object.
Syntax:
DateObj.toLocaleTimeString()
In the above syntax, DateObj is a valid Date object created using Date() constructor from which we want to fetch the time.
Parameter: This function does not take any parameter. It is just used along with a Date Object from which we want to fetch the time.
Return Values: It returns a string which is time from given Date object.
Below program illustrates the toLocaleTimeString() method:
<script> // Here a date has been assigned // while creating Date object var dateobj = new Date('July 16, 2018 05:35:32'); // time from above date object is // being extracted using toLocaleTimeString() var B = dateobj.toLocaleTimeString(); // Printing time document.write(B); </script> |
Output:
5:35:32
Errors and Exceptions:
- Example 1: The date of the month should lie in between 1 to 31 because none of the months have the date greater than 31 that is why it returns Invalid Date because the date for the month does not exist. Hence, Year will not have existed when the date of the month is given as 36 i.e, greater than 31.
<script>// Here a date has been assigned// while creating Date objectvardateobj =newDate('July 36, 2018 05:35:32');// time from above date object is// being extracted using toLocaleTimeString()varB = dateobj.toLocaleTimeString();// Printing timedocument.write(B);</script>chevron_rightfilter_noneOutput:
Invalid Date
- Example 2: If nothing as parameter is given to the Date() constructor the current date is passed in Date object and hence toLocaleTimeString() will return current time.
<script>// Here a date has been assigned// while creating Date objectvardateobj =newDate();// time from above date object is// being extracted using toLocaleTimeString()varB = dateobj.toLocaleTimeString();// Printing timedocument.write(B);</script>chevron_rightfilter_noneOutput:
The time returned in this program is Current time4:09:16
Supported Browsers: The browsers supported by JavaScript date.toLocaleTimeString() 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 | Data Types in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | JavaScript Prompt Example
- 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.



