JavaScript getTime Function

JavaScript getTime function is one a Date Function, which returns the Time from a given. This getTime function returns the total number of Milliseconds from January 1, 1970, to a given datetime. We are using getTime to return the number of Milliseconds from the default date to the current datetime.

<!DOCTYPE html>
<html>
<head>
    <title> JS </title>
</head>
<body>
    <h1> Example </h1>
<script>
  var dt = Date();  
  document.write("Date and Time : " + dt);
  document.write("Time : " + dt.getTime());
</script>
</body>
</html>
Example

Date and Time: Sun Nov 04 2018 15:48:43 GMT+0530 (Indian Standard Time)
Time : 1541326723757

JavaScript getTime Function Example

In this example, we are using JavaScrip get Time function on the custom date.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScriptGetTimeFunction </title>
</head>
<body>
    <h1> JavaScriptgetTimeFunction Example </h1>
<script>
  var dt = Date("December 22, 2002 10:11:43");
  document.write("Date and Time : " + dt);
  document.write("Time using getTime(): " + dt.getTime());
</script>
</body>
</html>
getTime Example

In this example, we are using get Time on the custom date without the time. This function considers the time as 00:00:00 and returns the milliseconds from the default time (1 January 1970) to the specified date.

<!DOCTYPE html>
<html>
<head>
    <title> JS </title>
</head>
<body>
    <h1> Example </h1>
<script>
  var dt = Date("December 22, 1972");
  document.write("DateTime : " + dt);
  document.write("Time : " + dt.getTime());
</script>
</body>
</html>
Example

DateTime: Fri Dec 22 1972 00:00:00 GMT+0530 (Indian Standard Time)
Time : 93810600000