Javascript Reference - JavaScript setTime() Method








setTime(milliseconds)
Sets the milliseconds representation of the date, thus changing the entire date.

Browser Support

setTime() Yes Yes Yes Yes Yes

Syntax

dateObject.setTime(millisec);

Parameter Values

Parameter Description
millisec Required. The number of milliseconds to be added to, or subtracted from, midnight January 1, 1970




Return Value

return a number representing the number of milliseconds between the date value and midnight January 1 1970.

Example


      var myDate = new Date();
      
      myDate.setTime(123);
      console.log(myDate.toString());
      

The code above generates the following result.

Example 2

The following code subtracts 1000 milliseconds from January 1, 1970, and display the new date and time:


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--  w  w  w .j  a  va  2  s  . c om-->
    var d = new Date();
    d.setTime(-1000);
    document.getElementById("demo").innerHTML = d;
}
</script>
</body>
</html>

The code above is rendered as follows: