JavaScript Date toLocaleTimeString() Method - GeeksforGeeks

22 Oct, 2021
Below is the example of Date toLocaleTimeString() method.
Example:<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>
<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 
5:35:32 
The date.toLocaleTimeString() method is used to fetch the time from a given Date object.Syntax:
DateObj.toLocaleTimeString()
Parameter: This method does not accept 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.
Note: The DateObj is a valid Date object created using Date() constructor from which we want to fetch the time.
More codes for the above method are as follows:
PRogram 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 object  var dateobj = new Date('July 36, 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:
Invalid Date
Program 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 object  var dateobj = new Date();     // time from above date object is  // being extracted using toLocaleTimeString()  var B = dateobj.toLocaleTimeString();     // Printing time  document.write(B);     </script>
Output: The time returned in this program is Current time
4:09:16 

Supported Browsers: The browsers supported by JavaScript Date toLocaleTimeString() Method are listed below:
Google Chrome 1and above
Edge 12 and above
Firefox 1 and above
Internet Explorer 5.5 and above
Opera 5 and above
Safari 1 and above
ysachin2314
javascript-date
JavaScript-Methods
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
JavaScript | Promises
How to get character array from string in JavaScript?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?