JavaScript Date toTimeString() Method

Below is the example of Date toTimeString() method.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  • 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


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).

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


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

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.