Javascript Reference - JavaScript setSeconds() Method








setSeconds(seconds)
Sets the date's seconds. Setting the seconds to a number greater than 59 also increments the minutes.

This method can also be used to set the milliseconds.

Browser Support

setSeconds() Yes Yes Yes Yes Yes

Syntax

dateObject.setSeconds(sec, millisec);

Parameter Values

Parameter Description
sec Required. An integer representing the seconds Expected values are 0-59, but other values are allowed:
-1 will be the last second of the previous minute
60 will be the first second of the next minute
millisec Optional. An integer representing the milliseconds Expected values are 0-999, but other values are allowed:
-1 will be the last millisecond of the previous second
1000 will be the first millisecond of the next second




Return Value

return a number representing the number of milliseconds from midnight January 1 1970.

Example


      var myDate = new Date();
      
      myDate.setSeconds(21);
      console.log(myDate.toString());
      

The code above generates the following result.

Example 2

The following code sets both the seconds and milliseconds:


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from w ww. j a va  2  s  .c  o m-->
    var d = new Date();
    d.setSeconds(32, 825);
    var s = d.getSeconds();
    var ms = d.getMilliseconds();
    var x = document.getElementById("demo");
    x.innerHTML = s + ":" + ms;
}
</script>
</body>
</html>

The code above is rendered as follows: