How to use setSeconds in JavaScript

In this article, I will explain use of setSeconds() method in JavaScript date object.
  • 1477
JavaScript setSeconds() method
  • setSeconds() method of date method sets the second for a given date according to local time.

  • setSeconds() method can also be used to set the milliseconds.

Syntax

Date.setSeconds(sec,milisec)

  • Sec : Sec is required parameter. An integer is representing the seconds and it's value between 0 and 59.

  • Mili : Mili is optional parameter. A number is representing the milliseconds and it's value between 0 and 999.

Example

In the following example show to setSeconds() method set the seconds to 20.

<!DOCTYPE html>

<html>

<body>

<h3 style="color:Teal ">JavaScript setSeconds() Method Example</h3>

<p id="demo">Click for display the date-time after changing the seconds</p>

<button onclick="myFunction()">Click On</button>

<script type="text/javascript">

    function myFunction() {

        var d = new Date();

        d.setSeconds(20);

        var x = document.getElementById("demo");

        x.innerHTML ="After changing the second, time is : "+ d;

    }

</script>

</body>

</html>

Output1

setSecond 1.jpg

Output2

setSecond 2.jpg

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.