Tutorialspoint.dev

JavaScript | date.getSeconds() method

The date.getSeconds() is an inbuilt function in JavaScript which is used to fetch the seconds from given Date object.

Syntax:

DateObj.getSeconds()

In the above syntax, DateObj is a valid Date object created using Date() conctructor from which we want to fetch seconds.

Parameter: This function does not takes any parameter. It is just used along with a Date Object from which we want to fetch seconds.

Return Values: It returns the second for the given date object. Seconds is an integer value ranging from 0 to 59.



Below program illustrate the getSeconds() method:

// Here a date has been assigned 
// while creating Date object
var DateObj = new Date('October 15, 1996 05:35:32');
  
// second from above Date object is being 
// extracted using getSeconds()
var sec = DateObj.getSeconds();
  
// Printing second
console.log(sec);

Output:

32

Errors and Exceptions:

  • Program 1: Here date of the month should must lie in between 1 to 31 because none of the month have date greater than 31 that is why it returns NaN i.e, not a number because if date for the month does not exist.
    // Here a date has been assigned 
    // while creating Date object
    var DateObj = new Date('October 33, 1996 05:35:32');
      
    // second from above Date object is being 
    // extracted using getSeconds()
    var sec = DateObj.getSeconds();
      
    // Printing second
    console.log(sec);

    Output:

    NaN
  • Program 2: If second is not given, it returns zero (0).
    // Here a date has been assigned 
    // while creating Date object
    var DateObj = new Date('October 13, 1996 05:35');
      
    // second from above Date object is being 
    // extracted using getSeconds()
    var sec = DateObj.getSeconds();
      
    // Printing second
    console.log(sec);

    Output:

    0
  • Program 3: If nothing as parameter is given to the Date() constructor, it returns current second.
    // Creating a Date Object
    var DateObj = new Date();
      
    // second from above Date object is being 
    // extracted using getSeconds()
    var sec = DateObj.getSeconds();
      
    // Printing second
    console.log(sec);

    Output:

    8
  • Program 4: If second is 88, it return 0 as exception because seconds range is in between 0 to 59 and 88 is out of this range.
    // Here a date has been assigned 
    // while creating Date object
    var DateObj = new Date('October 13, 1996 05:35:88');
      
    // second from above Date object is being 
    // extracted using getSeconds()
    var sec = DateObj.getSeconds();
      
    // Printing second
    console.log(sec);

    Output:

    0

Application: It has many applications such as getting current Second. Below program shows one of the application of this function. It gives the current Second.

// Creating Date Object
var DateObj = new Date();
  
// second from above Date Obect is
// being extracted using getSeconds()
var sec = DateObj.getSeconds();
  
// Printing second
console.log(sec);

Output:

8


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter