setTime(milliseconds)
Sets the milliseconds representation
of the date, thus changing the entire date.
setTime() |
Yes | Yes | Yes | Yes | Yes |
dateObject.setTime(millisec);
Parameter | Description |
---|---|
millisec | Required. The number of milliseconds to be added to, or subtracted from, midnight January 1, 1970 |
return a number representing the number of milliseconds between the date value and midnight January 1 1970.
var myDate = new Date();
myDate.setTime(123);
console.log(myDate.toString());
The code above generates the following result.
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: