Below is the example of Date getTime() method.
- Example:
<script>// Here a date has been assigned// while creating Date objectvarA =newDate('October 15, 1996 05:35:32');// hour from above is being// extracted using getTime()varB = A.getTime();// Printing time in milliseconds.document.write(B);</script>chevron_rightfilter_none - Output:
845337932000
The date.getTime() method method used to 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 the getTime() method is called on this date object it returns the number of milliseconds since 1 January 1970 (Unix Epoch).
Syntax:
Date.getTime()
Parameters: This method does not accept any parameter.
Return type: A numeric value equal to no of milliseconds since Unix Epoch.
More codes for the above method are as follows:
Program 1: Here the date of the month must lie in between 1 to 31 because no date can have month greater than 31. That is why it returns NaN i.e, Not a Number if the month in the Date object is greater than 31. Hours will not have existed when the date of the month given as 33 i.e, greater than 31.
<script> // Creating a Date object var A = new Date('October 35, 1996 12:35:32'); var B = A.getTime(); // Printing hour. document.write(B); </script> |
Output:
NaN
Program 2: Here we will calculate the user age by providing the birth date of the user.
<!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. </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> |
Output:

Supported Browsers: The browsers supported by JavaScript Date getTime() method are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
- How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
- PHP | IntlCalendar getTime() Function
- How to check the input date is equal to today's date or not using JavaScript ?
- How to convert UTC date time into local date time using JavaScript ?
- JavaScript Date toLocaleTimeString() Method
- JavaScript Date UTC() Method
- JavaScript Date getHours() Method
- JavaScript Date getMinutes() Method
- JavaScript Date getMonth() Method
- JavaScript Date getSeconds() Method
- JavaScript Date getDay() Method
- JavaScript Date getMilliseconds() Method
- JavaScript Date getFullYear() Method
- JavaScript Date getDate() Method
- JavaScript Date getUTCDate() Method
- JavaScript Date getUTCDay() Method
- JavaScript Date getUTCFullYear() Method
- JavaScript Date getUTCHours() Method
- JavaScript Date getUTCMilliseconds() Method
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.


