Wednesday, September 25, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Date toLocaleTimeString() Method

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




// Here a date has been assigned
// while creating Date object
let dateobj = new Date('July 16, 2018 05:35:32');
 
// time from above date object is
// being extracted using toLocaleTimeString()
let B = dateobj.toLocaleTimeString();
 
// Printing time
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




// Here a date has been assigned
// while creating Date object
let dateobj = new Date('July 36, 2018 05:35:32');
 
// time from above date object is
// being extracted using toLocaleTimeString()
let B = dateobj.toLocaleTimeString();
 
// Printing time
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




// Here a date has been assigned
// while creating Date object
let dateobj = new Date();
 
// time from above date object is
// being extracted using toLocaleTimeString()
let B = dateobj.toLocaleTimeString();
 
// Printing time
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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments