JavaScript getTime() Date method explained

Posted on May 11, 2021

Let's learn how the getTime() method works

The JavaScript getTime() method is a built-in method of the Date object that transforms your current date value to its Unix timestamp equivalent.

To use the method, you just need to call it on a Date object instance:

const date = new Date("01/20/2021");
console.log(date.getTime()); // 1611075600000

The getTime() method returns the number of milliseconds that have passed between midnight of January 1, 1970 and the specified date (January 20, 2021 in the example above).

You can optionally process the returned value further to calculate the timestamp in seconds, minutes, or hours:

const date = new Date("01/20/2021").getTime();
const dateInSeconds = Math.floor(date / 1000);
console.log(dateInSeconds); // 1611075600

const dateInMinutes = Math.floor(date / (1000 * 60));
console.log(dateInMinutes); // 26851260

const dateInHours = Math.floor(date / (1000 * 60 * 60));
console.log(dateInHours); // 447521

And that’s all to know about the getTime() method 😉

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.