The Wayback Machine - https://web.archive.org/web/20240119133358/https://www.geeksforgeeks.org/javascript-date-getseconds-method/
Open In App
Related Articles

JavaScript Date getSeconds() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 19 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials