Home >>JavaScript Date Object Methods >JavaScript getSeconds() Method

JavaScript getSeconds() Method

JavaScript getSeconds() Method

JavaScript getSeconds() method is used to return the seconds of the given input date and time. It returns an integer value between 0 to 59.

Syntax:
Date.getSeconds()

Browser Support

Method Chrome Edge Firefox Safari Opera
getSeconds() Yes Yes Yes Yes Yes
Here is an example of JavaScript getSeconds() method:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() 
{
  var d = new Date();
  var n = d.getSeconds();
  document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

Example 2:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction1()">Try it</button>
<p id="demo1"></p>
<script>
function addZero(i) 
{
  if (i < 10) 
{
    i = "0" + i;
}
  return i;
}
function myFunction1() 
{
  var d = new Date();
  var x = document.getElementById("demo1");
  var h = addZero(d.getHours());
  var m = addZero(d.getMinutes());
  var s = addZero(d.getSeconds());
  x.innerHTML = h + ":" + m + ":" + s;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.