Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

JavaScript getTime() Method



The getTime() method in JavaScript returns the number of milliseconds elapsed since 1st January 1970.

Following is the code for getTime() function −

Example

 Live Demo

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .sample {
      font-size: 18px;
      font-weight: 500;
   }
</style>
</head>
<body>
<h1>JavaScript getTime() Method</h1>
<div class="sample"></div>
<button class="Btn">CLICK HERE</button>
<h3>
Click on the above button to get the seconds elapsed since January 1st
1970
</h3>
<script>
   let sampleEle = document.querySelector(".sample");
   let date = new Date();
   document.querySelector(".Btn").addEventListener("click", () => {
      sampleEle.innerHTML = "Seconds elapsed since 01/01/1970 = " + date.getTime();
   });
</script>
</body>
</html>

Output

On clicking the “CLICK HERE” button −

Updated on: 2020-05-11T12:23:46+05:30

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements