How to Calculate the Time Between 2 Dates in TypeScript ? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we will calculate the time between two different dates using TypeScript. We will use the getTime() method to get the current time of the specified date. Syntax:const variable_name = new Date();const time = variable_name.getTime();Note: The getTime() method will return the time in the milliseconds. ApproachCreate two different dates using the Date object in TypeScript.Now, use the getTime() method on the created dates to get the time in milliseconds.After getting the time, subtract them to get the difference between them.Now, convert the resulting millisecond time into hours and minutes using formulas.Example 1: The below example will explain the use of the getTime() method to calculate the time between two dates. JavaScript // Future Date const firstDate: Date = new Date('2023-12-21'); // Current Date const secondDate: Date = new Date(); // Time Difference in Milliseconds const milliDiff: number = firstDate.getTime() - secondDate.getTime(); // Converting time into hh:mm:ss format // Total number of seconds in the difference const totalSeconds = Math.floor(milliDiff / 1000); // Total number of minutes in the difference const totalMinutes = Math.floor(totalSeconds / 60); // Total number of hours in the difference const totalHours = Math.floor(totalMinutes / 60); // Getting the number of seconds left in one minute const remSeconds = totalSeconds % 60; // Getting the number of minutes left in one hour const remMinutes = totalMinutes % 60; console.log(`${totalHours}:${remMinutes}:${remSeconds}`); Output: 14:19:59Example 2: The below example will show you the difference just like countdown using the setInterval() method. JavaScript const countDown = () => { // Future Date const firstDate: Date = new Date('2023-12-21'); // Current Date const secondDate: Date = new Date(); // Time Difference in Milliseconds const milliDiff: number = firstDate.getTime() - secondDate.getTime(); // Converting time into hh:mm:ss format // Total number of seconds in the difference const totalSeconds = Math.floor(milliDiff / 1000); // Total number of minutes in the difference const totalMinutes = Math.floor(totalSeconds / 60); // Total number of hours in the difference const totalHours = Math.floor(totalMinutes / 60); // Getting the number of seconds left in one minute const remSeconds = totalSeconds % 60; // Getting the number of minutes left in one hour const remMinutes = totalMinutes % 60; console.log(`${totalHours}:${remMinutes}:${remSeconds}`); } setInterval(countDown, 1000); Output: Comment More infoAdvertise with us Next Article How to Calculate the Time Between 2 Dates in TypeScript ? A abhish8rzd Follow Improve Article Tags : JavaScript Web Technologies TypeScript Similar Reads How to Calculate the Number of Days Between two Dates in TypeScript ? In TypeScript, calculating the number of days between two dates is a common task in many applications. Whether you're working with date ranges, scheduling, or performing date-based calculations, knowing how to accurately determine the difference in days between two dates is essential. Different appr 3 min read How to calculate minutes between two dates in JavaScript ? Given two dates and the task is to get the number of minutes between them using JavaScript. Approach: Initialize both Date object.Subtract the older date from the new date. It will give the number of milliseconds from 1 January 1970.Convert milliseconds to minutes. Example 1: This example uses the c 2 min read How to Calculate the Number of Days between Two Dates in JavaScript? Calculating the number of days between two dates is a common task in web development, especially when working with schedules, deadlines, or event planning. JavaScript provides a simple and effective way to perform this calculation by using different approaches. Whether you're comparing past and futu 4 min read How to Compare Two Date Strings in TypeScript ? In TypeScript, we can compare two date strings by converting them to comparable formats using Date objects or by parsing them into the timestamps. We will discuss different approaches with the practical implementation of each one of them. Table of ContentUsing Date ObjectsUsing Date.parseUsing Intl. 4 min read How to Convert String to Date in TypeScript ? In TypeScript, conversion from string to date can be done using the Date object and its method. We can use various inbuilt methods of Date object like new Date() constructor, Date.parse(), and Date.UTC.Table of ContentUsing new Date()Using Date.parse() Using Date.UTC()Using new Date()In this approac 2 min read Like