getSeconds(). This method is a part of the Date object and is used to retrieve the seconds (from 0 to 59) for a specified date and time.
Usage of getSeconds() Method
The syntax for using the getSeconds() method is quite straightforward:
var seconds = date.getSeconds();
This code snippet retrieves the current seconds from a Date object named ‘date’.
A Practical Example
Let’s look at an example where we use this method:
var date = new Date(); var seconds = date.getSeconds(); console.log("Current Seconds: " + seconds);
In this example, we first create a new Date object. Then, we use the getSeconds() method to retrieve the current second. Finally, we log it to console.
Note on Return Value
The return value of this function will be an integer between 0 and 59. If you call this function on a date object that does not have a specific time set (i.e., you only set year, month, day), it will default to returning 0.