When working with dates, knowing how to calculate the time between 2 dates in TypeScript is very important. If you are struggling and don’t know how to do so then please don’t worry. In this tutorial, we will show you some of the most common and easy ways to get it done.
Calculate the time between 2 dates in TypeScript
Let’s imagine we are having this problem: Karl is a student and he loves New Year because there will be no classes. So he wants to calculate the time between the current day and New Year’s day to see how much time he has to wait until then. So we have to build a program to help Karl with that.
First, let’s declare the current dates and the next new year date, using the new Date method.
There are many other ways to use the new Date() method, but for this problem, we only need these two:
- new Date(): use the current date and time to create a date object.
- new Date(yyyy-mm-dd): use a specified date to create a date object.
const currentDate = new Date(); // -> Fri Oct 28 2022 09:05:28 GMT+0700 (Indochina Time) const newYearDate = new Date(2023 - 1 - 1); // -> Thu Jan 01 1970 08:00:02 GMT+0800 (Indochina Time)
Now let’s come to our main part: Calculate the time between these two days. These are the possible solutions:
Get the difference in milliseconds
To calculate the time difference, we will use the getTime() method. The getTime() method will return the milliseconds of the date since January 1st, 1970.
- Date: the date that you want to convert into milliseconds.
Return value: The number of milliseconds since January 1st, 1970.
const currentDate = new Date(); // 10/29/2022 00:08:28 const newYearDate = new Date(2023 - 1 - 1); // 01/01/1970 08:00:02 // Get the difference in milliseconds const result = currentDate.getTime() - newYearDate.getTime(); console.log(result);
Get the time difference
Having the difference in milliseconds, we can use it to calculate the difference in hours, minutes and seconds.
const currentDate = new Date(); // 10/29/2022 00:08:28 const newYearDate = new Date(2023 - 1 - 1); // 01/01/1970 08:00:02 // Get the difference in milliseconds const milliSeconds = currentDate.getTime() - newYearDate.getTime(); // Get the number of hours var hours = Math.floor(milliSeconds / 1000 / 60 / 60); // Get the number of minutes var minutes = Math.floor(milliSeconds / 1000 / 60 - hours * 60); // Get the number of seconds const seconds = Math.floor( milliSeconds / 1000 - minutes * 60 - hours * 60 * 60 ); console.log(hours, "hours, ", minutes, "minutes, ", seconds, "seconds");
463049 hours, 35 minutes, 46 seconds
We use the Math.floor() method to round the number into an integer.
In this tutorial, we have shown you how to calculate the time between 2 dates in TypeScript. The idea is we will find the milliseconds difference first then convert it into different formats as we want.
Maybe you are interested:
- How to format date/time in typescript
- Get the current date and time in TypeScript
- How to add Days to a Date in TypeScript
Name of the university: VinUni