Javascript - Date getTime() Method

The getTime() method returns the number of milliseconds between midnight of January 1, 1970 and the date object.

Description

The getTime() method returns the number of milliseconds between midnight of January 1, 1970 and the date object.

Syntax

Date.getTime()

Parameters

None

Return

A Number, representing the number of milliseconds since midnight January 1, 1970

Example

Return the number of milliseconds since 1970/01/01:

Demo

//The internal clock in JavaScript starts at midnight January 1, 1970.
//display the number of milliseconds since midnight, January 1, 1970.

var d = new Date();
var n = d.getTime();
console.log(n);/*  w  w w  .j  a v  a2 s . c  om*/

Result

Calculate the number of years since 1970/01/01:

Demo

//calculate the number of years since midnight, January 1, 1970.

var minutes = 1000 * 60;
var hours = minutes * 60;
var days = hours * 24;
var years = days * 365;
var d = new Date();
var t= d.getTime();

var y = Math.round(t / years);
console.log(y);//from w ww  . j  a  v a  2 s .com

Result