Skip to main content

How to Subtract Days from a Date in JavaScript

A common requirement when working with dates is to calculate a new date by subtracting a certain number of days from a given date. This is essential for tasks like finding a date a week ago, calculating expiration dates, or filtering data within a time window. The native Date object in JavaScript provides simple methods to handle this, automatically managing complex edge cases like month and year rollovers.

This guide will teach you the modern, standard method for subtracting days from a Date object in an immutable way, which is a crucial best practice to avoid bugs in your code.

The Core Problem: Date Objects are Mutable

A critical concept to understand is that Date objects in JavaScript are mutable. Methods like setDate() modify the original Date object in place. Modifying objects or parameters directly can lead to unexpected side effects and bugs that are hard to track down.

The Problem: Mutating the Original Date

function subtractDays(date, days) {
date.setDate(date.getDate() - days);
return date;
}

const originalDate = new Date('2025-10-20T10:00:00.000Z');
const newDate = subtractDays(originalDate, 5);

console.log(newDate.toISOString()); // Output: '2025-10-20T10:00:00.000Z'
console.log(originalDate.toISOString()); // Output: '2025-10-20T10:00:00.000Z' (The original was changed!)
note

This is often not the desired behavior. The best practice is to always create a copy of the date before performing any modifications.

The Solution: A Non-Mutating Function

The correct and safe way to subtract days is to first create a copy of the date and then modify the copy. This is known as an immutable operation.

Solution: this function creates a new Date object, ensuring the original remains unchanged.

/**
* Returns a new Date object with the specified number of days subtracted.
* @param {Date} date - The original date.
* @param {number} days - The number of days to subtract.
* @returns {Date} A new Date object.
*/
function subtractDays(date, days) {
const dateCopy = new Date(date);
dateCopy.setDate(date.getDate() - days);
return dateCopy;
}

// Example Usage:
const originalDate = new Date('2025-10-20T10:00:00.000Z');

// Subtract 10 days from the original date
const newDate = subtractDays(originalDate, 10);

console.log('Original Date:', originalDate.toISOString());
console.log('New Date: ', newDate.toISOString());

Output:

Original Date: 2025-10-20T10:00:00.000Z
New Date: 2025-10-10T10:00:00.000Z
note

By creating a dateCopy, we ensure our function is "pure"—it doesn't have side effects and will always return a new date without altering the original.

How the setDate() Method Works

The magic of this operation lies in the setDate() method. It is smart enough to handle all date calculations for you, including rolling back to the previous month or year.

  • date.getDate(): Gets the day of the month (a number from 1 to 31).
  • date.setDate(day): Sets the day of the month.

When you pass a number that is zero or negative, setDate() automatically adjusts.

// Example: Crossing a month boundary
const firstOfMarch = new Date('2025-03-01');

// Subtracting 1 day from the 1st of March
firstOfMarch.setDate(firstOfMarch.getDate() - 1);

// The Date object correctly rolls back to the last day of February
console.log(firstOfMarch.toISOString()); // Output: '2025-02-28T00:00:00.000Z'
note

This built-in logic is what makes date arithmetic with setDate() so reliable.

Resetting the Time to Midnight

Sometimes, when you subtract days, you also want to get the date at the very beginning of that day (midnight). You can do this by chaining the setHours() method.

Solution: this function subtracts the days and then resets the time to 00:00:00.000.

function getDateXDaysAgoAtMidnight(days, date = new Date()) {
const dateCopy = new Date(date);
dateCopy.setDate(date.getDate() - days);

// Set time to the start of the day
dateCopy.setHours(0, 0, 0, 0);

return dateCopy;
}

const fiveDaysAgo = getDateXDaysAgoAtMidnight(5);

console.log(fiveDaysAgo); // Output: (A date object for 5 days ago at midnight)

Conclusion

Subtracting days from a Date in JavaScript is simple and reliable, as long as you follow the best practice of immutability.

  • Always create a copy of your Date object before modifying it to avoid unexpected side effects: const dateCopy = new Date(originalDate);.
  • Use the dateCopy.setDate(dateCopy.getDate() - days) pattern to perform the subtraction. The Date object will automatically handle month and year rollovers.
  • While third-party libraries like date-fns or Moment.js offer more advanced date manipulation features, using the native Date object is perfectly sufficient and highly efficient for simple arithmetic like adding or subtracting days.