Calculate the time between 2 Dates in TypeScript

avatar

Borislav Hadzhiev

Last updated: Mar 7, 2022

banner

Photo from Unsplash

Calculate the time between 2 Dates in TypeScript #

To calculate the time between 2 dates in TypeScript, call the getTime() method on both dates when subtracting, e.g. date2.getTime() - date1.getTime(). The getTime() method returns a number representing the milliseconds between the unix epoch an the given date.

index.ts
const date1 = new Date('2022-09-24'); const date2 = new Date('2022-09-25'); const msBetweenDates = date2.getTime() - date1.getTime(); console.log(msBetweenDates); // 👉️ 86400000

We used the getTime method to convert the dates to a unix timestamp.

The result of the subtraction is the number of milliseconds between the two dates.

You can easily convert the milliseconds to any other unit.

Here is an example that converts the difference between the dates (milliseconds) to time, formatted as hh:mm:ss.

index.ts
function padTo2Digits(num: number) { return num.toString().padStart(2, '0'); } function convertMsToTime(milliseconds: number) { let seconds = Math.floor(milliseconds / 1000); let minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); seconds = seconds % 60; minutes = minutes % 60; // 👇️ If you want to roll hours over, e.g. 00 to 24 // 👇️ uncomment the line below // uncommenting next line gets you `00:00:00` instead of `24:00:00` // or `12:15:31` instead of `36:15:31`, etc. // 👇️ (roll hours over) // hours = hours % 24; return `${padTo2Digits(hours)}:${padTo2Digits(minutes)}:${padTo2Digits( seconds, )}`; } console.log(convertMsToTime(54000000)); // 👉️ 15:00:00 (15 hours) console.log(convertMsToTime(86400000)); // 👉️ 24:00:00 (24 hours) console.log(convertMsToTime(36900000)); // 👉️ 10:15:00 (10 hours, 15 minutes) console.log(convertMsToTime(15305000)); // 👉️ 04:15:05 (4 hours, 15 minutes, 5 seconds)

We created a reusable function, which takes milliseconds as a parameter and returns the hours, minutes and seconds representation, formatted as hh:mm:ss.

The first thing we did is create a padTo2Digits function, which will take care of adding a leading zero if the hours, minutes or seconds only contain a single digit (are less than 10).
index.js
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } console.log(padTo2Digits(3)); // 👉️ '03' console.log(padTo2Digits(6)); // 👉️ '06' console.log(padTo2Digits(10)); // 👉️ '10'

We want to make sure the result does not alternate between single and double digit values depending on the hour, minutes and seconds.

In our convertMsToTime function, we:

  1. Converted the milliseconds to seconds by dividing the value by 1000.
  2. Converted the seconds to minutes by dividing the value by 60.
  3. Converted the minutes to hours by dividing the value by 60.
  4. (Optionally) - you can use the modulo (%) operator to reset the values to 0. If, for example, the user passed 86400000 as the milliseconds, which is equivalent to 24 hours.

You can uncomment out the hours = hours % 24 line to roll the hours over.

The last step is to format the values for the hours, minutes and seconds in a way that suits your use case.

We formatted them as hh:mm:ss in the example, by adding a leading zero if the value is less than 10, however you can tweak the return value of the function depending on your use case.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.