Javascript Reference - JavaScript getTime() Method








getTime()
Returns the milliseconds representation of the date; same as valueOf().

Browser Support

getTime() Yes Yes Yes Yes Yes

Syntax

dateObject.getTime();

Parameters

None

Return Value

return a number representing the number of milliseconds since midnight January 1, 1970.





Example

The internal clock in JavaScript starts at midnight January 1, 1970.

The following code displays the number of milliseconds since midnight, January 1, 1970.


var myDate = new Date();
console.log(myDate.getTime());

The code above generates the following result.

Example 2

The following code calculates the number of years since 1970/01/01:


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from   w w  w . ja v a 2s .c o  m-->
    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);

    document.getElementById("demo").innerHTML = y;
}
</script>

</body>
</html>

The code above is rendered as follows: