Below is the example of Date toTimeString() method.
- Example:
<script>// Here a date has been assigned// while creating Date objectvardateobj =newDate('October 15, 1996 05:35:32');// Contents of above date object// of time portion is returned// using toTimeString() function.varB = dateobj.toTimeString();// Printing the time.document.write(B);</script>chevron_rightfilter_none - Output:
05:35:32 GMT+0530 (India Standard Time)
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 Date() constructor whose time portion contents are returned in English like language.
Return Values: It return the time portion of the given date object in english.
Note: The DateObj is a valid Date object created using Date() constructor whose time portion contents is returned.
More codes for the above method are as follows:
Program 1: Here nothing as parameter is passed while creating date object but still toTimeString() method return current time.
<script> // Here nothing has been assigned // while creating Date object var dateobj = new Date(); // It return current time using // toTimeString() method. var B = dateobj.toTimeString(); // Printing the current time. document.write(B); </script> |
Output:
14:58:08 GMT+0530 (India Standard Time)
Program 2: When time part is not given to the parameter of Date() constructor then it return the time as zeros(0).
<script> // Only month date and year are // assigned without time while // creating Date object. var dateobj = new Date('October 15, 1996'); // It returns time using // toTimeString() method. var B = dateobj.toTimeString(); // Printing the time. document.write(B); </script> |
Output:
00:00:00 GMT+0530 (India Standard Time)
Supported Browsers: The browsers supported by JavaScript Date toTimeString() Method are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- JavaScript Date now() Method
- JavaScript Date UTC() Method
- JavaScript Date setUTCSeconds() Method
- JavaScript Date setUTCFullYear() Method
- JavaScript Date getTimezoneOffset() Method
- JavaScript Date setUTCDate() Method
- JavaScript Date getUTCSeconds() Method
- JavaScript Date getUTCMilliseconds() Method
- JavaScript Date setUTCHours() Method
- JavaScript Date setUTCMilliseconds() Method
- JavaScript Date getUTCHours() Method
- JavaScript Date getUTCFullYear() Method
- JavaScript Date toLocaleDateString() Method
- JavaScript Date toString() Method
- JavaScript Date getFullYear() Method
- JavaScript Date getMilliseconds() Method
- JavaScript Date parse() Method
- JavaScript Date getDay() Method
- JavaScript Date getUTCDay() Method
- JavaScript Date getUTCDate() Method
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.


