getTime method

The getTime method is applied to the date object and returns the number of milliseconds since midnight of January 1, 1970 prior to the given point in time. This date format is called timestamp.

Syntax

javascriptdate.getTime();

Example

Let's output the number of milliseconds from 1 January 1970 to the current time:

javascriptlet date = new Date();
let res = date.getTime();

console.log(res);

Example

This example will output the number of milliseconds from 1 January 1970 to the given time:

javascriptlet date = new Date(2015, 23, 4, 12, 59, 59);
let res = date.getTime();

console.log(res);

Example

Let's get the difference in milliseconds between the current and the given point in time:

javascriptlet now = new Date();
let date = new Date(2015, 23, 4, 12, 59, 59);
let diff = now.getTime() - date.getTime();

console.log(diff);

See also

  • the Date.now method
    that also obtains the timestamp