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

JavaScript setTime() Method

JavaScript setTime() Method

JavaScript setTime() method in JavaScript is used to sets a date and time represented by a number of milliseconds to/from midnight January1, 1970, 00:00:00 UTC.

Syntax:
Date.setTime(millisec)

Parameter Values

Parameter Description
millisec It is Required and an integer representing the number of milliseconds since 1 January 1970.

Browser Support

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

Example 2:
<html>
<body>
<button onclick="mysetTime1()">click me</button>
<p id="time1"></p>
<script>
function mysetTime1() 
{
  var z = new Date();
  z.setTime(-1332403882588);
  document.getElementById("time1").innerHTML = z;
}
</script>
</body>
</html>
Output:


No Sidebar ads