JavaScript Date toLocaleTimeString() Method
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 the time from the given Date object.
Note: The DateObj is a valid Date object created using the Date() constructor from which we want to fetch the time.
Below are examples of the Date toLocaleTimeString() method.
Example:
javascript
let dateobj = new Date( 'July 16, 2018 05:35:32' );
let B = dateobj.toLocaleTimeString();
console.log(B);
|
Output:
5:35:32
Example 2: The date of the month should lie between 1 to 31 because none of the months have a date greater than 31 that is why it returns an Invalid Date because the date for the month does not exist. Hence, the Year will not have existed when the date of the month is given as 36 i.e., greater than 31.
javascript
let dateobj = new Date( 'July 36, 2018 05:35:32' );
let B = dateobj.toLocaleTimeString();
console.log(B);
|
Output:
Invalid Date
Example 3: If nothing as a parameter is given to the Date() constructor the current date is passed in the Date object and hence toLocaleTimeString() will return the current time.
javascript
let dateobj = new Date();
let B = dateobj.toLocaleTimeString();
console.log(B);
|
Output:
4:09:16
We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article.
Supported Browsers: The browsers supported by JavaScript Date toLocaleTimeString() method are listed below:
- Google Chrome
- Internet Explorer
- Mozilla Firefox
- Opera
- Safari
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.
Last Updated :
22 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...