Subtract Days From a Date in TypeScript

Recently, while working on a TypeScript project, I needed to find a date a few days before an important event, like when to start preparing or send reminders.

If I had changed the dates directly, it could have caused issues in the code. So, I created a reusable function that subtracts days using TypeScript’s Date methods.

In this TypeScript tutorial, I’ll explain how to subtract days from a date in TypeScript using functions and external libraries.

Understanding Date Manipulation in TypeScript

Manipulating dates is a common requirement in many applications, from scheduling events to calculating deadlines. TypeScript, being a superset of JavaScript, inherits all the date manipulation capabilities of JavaScript. This makes it easy to work with dates using the built-in Date object.

Why Subtract Days from a Date in TypeScript?

There are numerous scenarios where you might need to subtract days from a date. For instance:

  • Project Management: Calculating deadlines and milestones.
  • Event Planning: Determining the start date of an event based on the end date.
  • Financial Applications: Calculating due dates for payments.

Check out: Check for Invalid Dates in TypeScript

Basic Date Subtraction in TypeScript

Let’s start with a simple example. Suppose you have a project deadline on June 30, 2025, and you need to find the date 10 days before the deadline.

const deadline = new Date('2025-06-30');
const daysToSubtract = 10;

// Subtract days
deadline.setDate(deadline.getDate() - daysToSubtract);

console.log(deadline); // Output: 2025-06-20

Output:

Subtract days from date in TypeScipt

In this example, we use the setDate and getDate methods of the Date object. The getDate method retrieves the day of the month, and setDate sets the day of the month. By subtracting the desired number of days, we can easily find the new date.

Handling Edge Cases

When working with dates, it’s important to handle edge cases, such as crossing month boundaries. TypeScript’s Date object automatically manages these cases.

Consider the following example where we subtract 15 days from December 5, 2025:

const eventDate = new Date('2025-12-05');
const daysToSubtract = 15;

// Subtract days
eventDate.setDate(eventDate.getDate() - daysToSubtract);

console.log(eventDate); // Output: 2025-11-20

Output:

TypeScript subtract days from dates

Here, the Date object correctly adjusts the month when the subtraction crosses the month boundary.

The output will be returned in datetime format as 2025-11-20T00:00:00.000Z . If you want the output as 2025-11-20, you need to format the date from a Date object into a string of the form YYYY-MM-DD.

Advanced Date Subtraction Techniques in TypeScript

While the basic method of subtracting days is straightforward, there are more advanced techniques and use cases that you might encounter.

Using Helper Functions

For better code readability and reusability, you can create a helper function to subtract days from a date.

function subtractDays(date: Date, days: number): Date {
    const result = new Date(date);
    result.setDate(result.getDate() - days);
    return result;
}

const projectDeadline = new Date('2025-05-30');
const newDeadline = subtractDays(projectDeadline, 10);

console.log(newDeadline); // Output: 2025-05-20

Output:

Subtract number of days from date in Typescript

This function encapsulates the logic of date manipulation, making your code cleaner and easier to maintain.

Check out: Convert Number to Date in TypeScript

Working With Time Zones

When dealing with dates, especially in applications used across different time zones, it’s important to consider time zone differences. The Date object in JavaScript and TypeScript uses the local time zone of the environment where the code is running.

If you need to work with UTC dates, you can use methods like getUTCDate and setUTCDate.

const utcDate = new Date(Date.UTC(2025, 3, 30)); // March 30, 2025, in UTC
const daysToSubtract = 10;

// Subtract days in UTC
utcDate.setUTCDate(utcDate.getUTCDate() - daysToSubtract);

console.log(utcDate); // 2025-04-20T00:00:00.000Z

Output:

Subtracting days from date in Typescript

Check out:  Get the Day of the Month in TypeScript

Using Date Libraries

While TypeScript’s built-in Date object is powerful, there are libraries like date-fns and moment.js that provide more advanced date manipulation capabilities.

Using date-fns

date-fns is a modern JavaScript date utility library that provides a variety of functions for date manipulation.

import { subDays } from 'date-fns';

const result = subDays(new Date('2025-03-30'), 10);
console.log(result); // Output: 2025-03-20T00:00:00.000Z

Output:

Subtract days using Date Libraries in Typescript

Using the moment.js library

Another popular date library that simplifies date manipulation in TypeScript is moment.js.

import moment from 'moment';

const result = moment('2025-05-25').subtract(10, 'days').toDate();
console.log(result); // Output: 2025-05-14T18:30:00.000Z

Output:

Subtract days from date in Typescript using library

Both libraries offer robust solutions for complex date manipulation scenarios and are worth considering for your projects.

Practical Examples to Subtract Days From Date in TypeScript

Example 1: Scheduling a Meeting

Suppose you need to schedule a meeting 7 days before Thanksgiving (November 27, 2025).

const thanksgiving = new Date('2025-11-27');
const daysBefore = 7;

function subtractDays(date: Date, days: number): Date {
  const result = new Date(date);
  result.setDate(result.getDate() - days);
  return result;
}

const meetingDate = subtractDays(thanksgiving, daysBefore);
console.log(meetingDate.toDateString()); // Output: Wed Nov 19 2025

Output:

Typescript subtract number of days from a date

Example 2: Calculating Payment Due Date

Imagine you have an invoice due on December 15, 2024, and you want to send a reminder 5 days before the due date.

const dueDate = new Date('2025-06-15');
const reminderDays = 5;

const reminderDate = subtractDays(dueDate, reminderDays);
console.log(reminderDate); // 2025-06-10

Output:

Subtract days from date value in Typescript

Check out: Format Dates in TypeScript

Example 3: Event Planning

You are planning an event on July 4, 2025, and need to start preparations 30 days in advance.

const eventDate: Date = new Date('2025-07-04');
const preparationDays: number = 30;

function subtractDays(date: Date, days: number): Date {
  const result = new Date(date);
  result.setDate(result.getDate() - days);
  return result;
}

const startPreparationDate: Date = subtractDays(eventDate, preparationDays);
console.log("start preparation at", startPreparationDate); // Output: 2025-06-04

Output:

Subtract days in Typescript using date object

Conclusion

In this tutorial, we have learned how to subtract days from a date in TypeScript using built-in Date methods and handle edge cases like crossing over to a previous month. We also covered how to work with UTC dates and explored external libraries like date-fns and moment.js that make date manipulation even easier.

With these methods, you can efficiently manage date-related event schedules in your TypeScript projects where you need to show the prior date.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.