JavaScript | date.toTimeString() function

The date.toTimeString() is an inbuilt function in JavaScript which 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()

Note: In the above syntax, DateObj is a valid Date object created using Date() constructor whose time portion contents is returned.



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

    Return Values: It return the time portion of the given date object in english.

    Below program illustrate the date.toTimeString() function:-

    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)


      Exceptional cases associated with this function:

    1. Here nothing as parameter is passed while creating date object but still toTimeString() function 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() function.
      var B = dateobj.toTimeString();
        
      // Printing the current time.
      document.write(B);
      </script>

      chevron_right

      
      

      Output:

      14:58:08 GMT+0530 (India Standard Time)
    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() function.
      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() Function are listed below:

    • Google Chrome
    • Internet Explorer
    • Firefox
    • Opera
    • Safari


    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.