Javascript - Date setTime() Method

The setTime() method sets a date and time by adding/subtracting a specified number of milliseconds to/from midnight January 1, 1970.

Description

The setTime() method sets a date and time by adding/subtracting a specified number of milliseconds to/from midnight January 1, 1970.

Syntax

Date.setTime(millisec)

Parameter Values

Parameter Require 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 object and midnight January 1 1970

Example

Add 12345678 milliseconds to January 1, 1970, and display the new date and time:

Demo

//display the date after setting the time.
var d = new Date();
d.setTime(12345678);/*from ww w.j a va2  s  .c o m*/
console.log(d);

//Subtract 12345678 milliseconds from January 1, 1970, and display the new date and time:
var d = new Date();
d.setTime(-12345678);
console.log(d);

Result