getTime()
Returns the milliseconds representation of
the date; same as valueOf().
getTime() |
Yes | Yes | Yes | Yes | Yes |
dateObject.getTime();
None
return a number representing the number of milliseconds since midnight January 1, 1970.
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.
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: