JavaScript getSeconds(): Get the seconds (0-59)

The JavaScript getSeconds() method is used to get the seconds (0-59) of the time. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <p id="xyz"></p>

   <script>
      const d = new Date();
      let min = d.getSeconds();
      document.getElementById("xyz").innerHTML = min;
   </script>

</body>
</html>
Output

In the above example, the following JavaScript statement:

const d = new Date();

creates a new "Date" object with the "new Date()" constructor. This method generates a new date and time object using the current system clock time.

Then the following JavaScript statement:

let min = d.getSeconds();

calls the Date object's getSeconds() method to get the current number of seconds (0-59). The min variable stores this value.

Finally, the following JavaScript statement:

document.getElementById("xyz").innerHTML = min;

finds the HTML element with the id attribute "xyz" and sets its innerHTML property to the value stored in the min variable using the document.getElementById() method. The current number of seconds will be displayed on the web page.

JavaScript getSeconds() syntax

The syntax of the getSeconds() method in JavaScript is:

x.getSeconds()

where x must be an object of the Date() constructor.

The getSeconds() method returns a number from 0 to 59, which will be the seconds of a date.

Please note: To display the date in the format dd-mm-yyyy, refer to its separate example.

Please note: To display time in the format hh:mm:ss, refer to its separate example.

Please note: To display time in the format hh:mm:ss AM/PM, refer to its separate example.

Advantages of the getSeconds() method in JavaScript

Disadvantages of the getSeconds() method in JavaScript

JavaScript Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!