As a developer, you may often encounter scenarios where you need to calculate the difference between two dates. Whether you’re building a calendar app, a scheduling system, or any other application that involves date manipulation, knowing how to subtract dates is crucial.
In this tutorial, I will explain how to subtract dates in TypeScript, along with practical examples to help you understand the concepts better.
Prerequisites
Before we dive into the details of subtracting dates in TypeScript, make sure you have the following:
- Basic understanding of TypeScript syntax and concepts
- TypeScript development environment set up
- Familiarity with the JavaScript
Dateobject
Subtracting Dates Using the ‘-‘ Operator
The simplest way to subtract dates in TypeScript is by using the - operator. When you subtract one date from another using this operator, the result is the difference between the two dates in milliseconds. Here’s an example:
const startDate = new Date('2025-05-15');
const endDate = new Date('2025-05-20');
const diffInMilliseconds = endDate - startDate;
console.log(diffInMilliseconds); // Output: 432000000In this example, we have two dates: startDate set to May 15, 2025, and the endDate set to May 20, 2025. By subtracting startDate from endDate, we get the difference in milliseconds, which is 432000000.
To convert the milliseconds to days, you can divide the result by the number of milliseconds in a day (24 * 60 * 60 * 1000):
const diffInDays = diffInMilliseconds / (24 * 60 * 60 * 1000);
console.log(diffInDays); // Output: 5This calculation shows that the difference between May 15, 2025, and May 20, 2025 is 5 days.
Check out: How to Check if a Date is Today in TypeScript
Using the getTime() Method
Another approach to subtract dates in TypeScript is by using the getTime() method of the Date object. This method returns the number of milliseconds since January 1, 1970. By subtracting the timestamps of two dates, you can calculate the difference between them. Here’s an example:
const startDate = new Date('2025-07-04');
const endDate = new Date('2025-07-10');
const diffInMilliseconds = endDate.getTime() - startDate.getTime();
const diffInDays = diffInMilliseconds / (24 * 60 * 60 * 1000);
console.log(diffInDays); // Output: 6Output:

In this example, we have two dates: startDate set to July 4, 2025 (Independence Day in the USA), and endDate set to July 10, 2025. By using the getTime() method, we obtain the timestamps of both dates and subtract them to get the difference in milliseconds. Finally, we convert the milliseconds to days as we did in the previous example.
Check out: How to Get Tomorrow’s Date in TypeScript
Subtracting Specific Time Units
Sometimes, you may need to subtract specific time units from a date, such as days, months, or years. TypeScript provides methods to manipulate dates based on these time units. Let’s explore a few examples.
Subtracting Days
To subtract a specific number of days from a date, you can use the setDate() method along with the getDate() method. Here’s an example:
const currentDate = new Date('2025-09-15');
const numberOfDaysToSubtract = 7;
const priorDate = new Date(currentDate);
priorDate.setDate(currentDate.getDate() - numberOfDaysToSubtract);
console.log(priorDate); // Output: Fri Sep 08 2025Output:

In this example, we have a currentDate set to September 15, 2025. We want to subtract 7 days from this date. To achieve this, we create a new Date object called priorDate and initialize it with the currentDate. Then, we use the setDate() method to subtract the desired number of days from the priorDate by getting the current date using getDate() and subtracting numberOfDaysToSubtract.
Subtracting Months
To subtract months from a date, you can use the setMonth() method in a similar manner. Here’s an example:
const currentDate = new Date('2025-11-27');
const numberOfMonthsToSubtract = 2;
const priorDate = new Date(currentDate);
priorDate.setMonth(currentDate.getMonth() - numberOfMonthsToSubtract);
console.log(priorDate);Output:

In this example, we have a currentDate set to November 27, 2025 (Thanksgiving in the USA). We want to subtract 2 months from this date. We create a new Date object called priorDate, initialize it with the currentDate, and then use the setMonth() method to subtract the desired number of months.
Subtracting Years
Subtracting years from a date follows a similar approach using the setFullYear() method. Here’s an example:
const currentDate = new Date('2025-12-25');
const numberOfYearsToSubtract = 5;
const priorDate = new Date(currentDate);
priorDate.setFullYear(currentDate.getFullYear() - numberOfYearsToSubtract);
console.log(priorDate); Output:

In this example, we have a currentDate set to December 25, 2025 (Christmas Day). We want to subtract 5 years from this date. We create a new Date object called priorDate, initialize it with the currentDate, and then use the setFullYear() method to subtract the desired number of years.
Handling Edge Cases
When subtracting dates, it’s important to consider edge cases to ensure accurate results. Here are a few scenarios to keep in mind:
- Subtracting dates that result in negative values
- Subtracting dates across leap years
- Subtracting dates that exceed the maximum or minimum date range
TypeScript’s built-in Date object handles most of these edge cases automatically. However, it’s always a good practice to validate the input dates and handle any exceptional cases based on your specific requirements.
Conclusion
In this tutorial, we explored various methods to subtract dates in TypeScript. We learned how to use the '-‘ operator to calculate the difference between two dates in milliseconds and convert it to days. We also explored using the getTime() method to subtract dates using timestamps. Additionally, we covered subtracting specific time units, such as days, months, and years, using the setDate(), setMonth(), and setFullYear() methods, respectively.
By applying these techniques, you can effectively subtract dates in your TypeScript applications, whether you’re working on a calendar app, a scheduling system, or any other project that involves date manipulation.
You may also like to read:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.