JavaScript setTime Function

JavaScript setTime function is a Date Function, which is used to set the Time by adding user-specified milliseconds to a default date, January 1, 1970 00:00:00. Here, you have to specify the time in Milliseconds, and it is an optional argument. The syntax of the JavaScript setTime function is:

 Date.setTime(Milliseconds)

JavaScript setTime Function Example

Here, we are using this setTime function to set the Milliseconds to 20000000.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Set Time Functions </title>
</head>
<body>
    <h1> JavaScript setTime Function Example </h1>
<script>
  var dt = Date();  
  document.write("Date and Time : " + dt + "<br/>");

  dt.setTime(20000000);
  document.write("After setTime() : " + dt);
</script>
</body>
</html>
JavaScript setTime Function Example

Date and Time: Thu Nov 08 2018 12:06:15 GMT+0530 (Indian Standard Time)
After setTime(): Thu Jan 01 1970 11:03:20 GMT+0530 (Indian Standard Time)

JavaScript set Time Example 2

In this JavaScript set Time function example, we set the Time of a custom date to 2000 milliseconds.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Set Time Functions </title>
</head>
<body>
    <h1> JavaScript setTime Function Example </h1>
<script>
  var dt = Date("January 1, 2017 23:45:22");
  document.write("Date and Time : " + dt + "<br/>");

  dt.setTime(2000);
  document.write("After setTime() : " + dt);
</script>
</body>
</html>
JavaScript setTime Function 2