Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Date toTimeString() Method

JavaScript Date toTimeString() Method

The date.toTimeString() method is used to return the time portion of the given date object in English. The date object is created using date() constructor.

Syntax:

dateObj.toTimeString()

Parameters: This function does not accept any parameter. It is just used along with a Date object created using the Date() constructor whose time portion contents are returned in English-like language. 

Return Values: It returns the time portion of the given date object in English. 

Note: The DateObj is a valid Date object created using the Date() constructor whose time portion contents are returned.

Below are examples of Date.toTimeString() method.

Example 1: 

javascript




// Here a date has been assigned
// while creating Date object
let dateobj =
    new Date('October 15, 1996 05:35:32');
 
// Contents of above date object
// of time portion is returned
// using toTimeString() function.
let B = dateobj.toTimeString();
 
// Printing the time.
console.log(B);


Output:

05:35:32 GMT+0530 (India Standard Time)

Example 2: Here nothing as a parameter is passed while creating the date object but still toTimeString() method returns the current time.

javascript




// Here nothing has been assigned
// while creating Date object
let dateobj = new Date();
 
// It return current time using
// toTimeString() method.
let B = dateobj.toTimeString();
 
// Printing the current time.
console.log(B);


Output:

14:58:08 GMT+0530 (India Standard Time)

Example 3: When the time part is not given to the parameter of the Date() constructor then it returns the time as zeros(0). 

javascript




// Only month date and year are
// assigned without time while
// creating Date object.
let dateobj = new Date('October 15, 1996');
 
// It returns time using
// toTimeString() method.
let B = dateobj.toTimeString();
 
// Printing the time.
console.log(B);


Output:

00:00:00 GMT+0530 (India Standard Time)

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 toTimeString() 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