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

JavaScript setMilliseconds() Method

JavaScript setMilliseconds() Method

JavaScript setMilliseconds() method is a Date method, which is used to set the Milliseconds of a given date on the basis of local time.

Syntax:
Date.setMilliseconds(millisec)

Parameter Values

Parameter Description
millisec It is Required. An integer between 0-999, representing the milliseconds
  • If the provided milliseconds value is -1 will result in the last millisecond of the previous second
  • If the provided milliseconds value is 1000 will result in the first millisecond of the next second
  • If the provided milliseconds value is 1001 will result in the second millisecond of the next second

Browser Support

Method Chrome Edge Firefox Safari Opera
setMilliseconds() Yes Yes Yes Yes Yes
Here is an Example of setMilliseconds() Method:
<html>
<body>
<button onclick="mysetMillisec()">click me</button>
<p id="setSec"></p>
<script>
function mysetMillisec() 
{
  var x = new Date();
  x.setMilliseconds(1010);
  var z = x.getMilliseconds();
  document.getElementById("setSec").innerHTML = z;
}
</script>
</body>
</html>
Output:

Example 2:
<html>
<body>
<script>
var milliSec1=new Date();	
document.writeln("Current millisecond : "+milliSec1.getMilliseconds()+"<br>");
milliSec1.setMilliseconds(-1);	
document.writeln("Updated millisecond : "+milliSec1.getMilliseconds());
</script>
</body>
</html>
Output: