Open In App

JavaScript Date getSeconds() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The date.getSeconds() method is used to fetch the seconds from the given Date object according to the local time. The value returned by this method ranges from 0 to 59.

Syntax: 

DateObj.getSeconds()

Parameter: This function does not accept any parameter. 

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

Example 1: Below is an example of Date.getSeconds() method. 

javascript




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


Output: 

32

Example 2: Here the date of the month should lie between 1 to 31 because none of the months have a date greater than 31 that is why it returns NaN i.e, not a number because if the date for the month does not exist. 

javascript




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


Output: 

NaN

Example 3: If a second is not given, it returns zero (0). 

javascript




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


Output: 

0

Example 4: If nothing as a parameter is given to the Date() constructor, it returns the current second. 

javascript




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


Output: 

8

Example 5: If the second is 88, it returns 0 as an exception because the second’s range is between 0 to 59, and 88 is out of this range. 

javascript




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


Output: 

0

We have a complete list of Javascript Javascript Date methods, to check those please go through the Javascript Date Object Complete Reference article.

Supported Browsers: The browsers supported by JavaScript Date.getSeconds() method are listed below: 

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 4 and above
  • Opera 3 and above
  • Safari 1 and above


Last Updated : 19 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads