Tutorialspoint.dev

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() conctructor 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:-

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

/div>

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

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

    Output:

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


This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter