Home >>JavaScript Date Object Methods >JavaScript getTime() Method

JavaScript getTime() Method

JavaScript getTime() Method

The get Time() Method returns the number of milliseconds since 1 January 1970 and the specified date. It is a method od Date object in JavaScript.

Syntax:
Date.getTime()

Browser Support

Method Chrome Edge Firefox Safari Opera
getTime() Yes Yes Yes Yes Yes
Here is an Example of JavaScript getTime() Method:
<html>
<body>
<button onclick="mygetTime()">Click me</button>
<p id="GetTime"></p>
<script>
function mygetTime() 
{
  var d = new Date();
  var x = d.getTime();
  document.getElementById("GetTime").innerHTML = x;
}
</script>
</body>
</html>
Output:

Example 2:
<html> 
<head> 
<title>Document</title> 
</head> 
<body> 
<div> 
<h4>Birth Date: 
<span class="date"> 
</span> 
</h4> 
</div> 
<div> 
<h4>getTime(): 
<span class="time"> 
</span> 
</h4> 
</div> 
<div>
<h4>I am 
<span class="years"></span>
<span> years old</span> 
</h4> 
</div> 
</body> 
<script> 
	var BD = new Date("Aug 27, 2002 22:15:27"); 
	var date = document.querySelector(".date"); 
	var time = document.querySelector(".time"); 
	var Today = new Date(); 
	var today = Today.getTime(); 
	var GT = BD.getTime(); 
	var year = 1000 * 60 * 60 * 24 * 365; 
	var years = (today - GT) / year; 
	date.innerHTML = BD; 
	time.innerHTML = BD.getTime(); 
	var x = document.querySelector(".years"); 
	x.innerHTML = Math.round(years); 
</script> 
</html>
Output:

Birth Date:

getTime():

I am years old


No Sidebar ads