JavaScript Date getTime() Method is used to return 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. The getTime() always uses UTC for time representation.
Syntax:
Date.getTime();
Parameters:
This method does not accept any parameter.
Return type:
A numeric value equal to no of milliseconds since Unix Epoch.
Example 1: In this example, the Date.getTime() method.
Javascript
let A = new Date('October 15, 1996 05:35:32');
let B = A.getTime();
console.log(B);
|
Example 2: Here, the date of the month must lie between 1 to 31 because no date can have a 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 is given as 33 i.e, greater than 31.
Javascript
let A = new Date('October 35, 1996 12:35:32');
let B = A.getTime();
console.log(B);
|
Example 3: Here, we will calculate the user’s age by providing the birth date of the user as BD.
Javascript
let BD = new Date("July 29, 1997 23:15:20");
let Today = new Date();
let today = Today.getTime();
let bd = BD.getTime();
let year = 1000 * 60 * 60 * 24 * 365;
let years = (today - bd) / year;
console.log(Math.round(years));
|
Supported Browsers:
- Google Chrome 1.0
- Microsoft Edge 12.0
- Firefox 1.0
- Internet Explorer 4.0
- Opera 3.0
- Safari 1.0
We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article.
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
15 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...