JavaScript | getTime() Method

The getTime() method is a method of Date object in Javascript. This method returns the number of milliseconds since 1 January 1970. when a new Date object is created it stores the date and time data when it is created. When getTime() method is called on this date object it returns the number of milliseconds since 1 January 1970 (Unix Epoch).

Syntax:

Date.getTime()

Return type: A numeric value equal to no of seconds since Unix Epoch.



Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    
    <meta http-equiv="X-UA-Compatible" 
          content="ie=edge">
    
    <title>Document</title>
</head>
  
<body>
    <div>
        <h4>Today's Date:
          <span class="date">
          </span>
      </h4>
    </div>
    <div>
        <h4>getTime(): 
          <span class="time">
          </span>
      </h4>
    </div>
</body>
<script>
    var Today = new Date();
    var date = 
        document.querySelector(".date");
    
    var time = 
        document.querySelector(".time");
  
    time.innerHTML = Today.getTime();
    date.innerHTML = Today;
</script>
  
</html>

chevron_right


Output:

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, 
                   initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" 
          content="ie=edge">
    <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> years old.
      </span>
  </h4>
    </div>
</body>
<script>
    var BD = new Date("July 29, 1997 23:15:20");
    var date = document.querySelector(".date");
    var time = document.querySelector(".time");
    var Today = new Date();
    var today = Today.getTime();
    var bd = BD.getTime();
    var year = 1000 * 60 * 60 * 24 * 365;
    var years = (today - bd) / year;
    date.innerHTML = BD;
    time.innerHTML = BD.getTime();
    var y = document.querySelector(".years");
    y.innerHTML = Math.round(years);
</script>
  
</html>

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.