How to Subtract Days From a Date in JavaScript

Last updated on October 11, 2022
How to Subtract Days From a Date in JavaScript

1. Date setDate() and getDate() methods

To subtract days from a Date in JavaScript:

  1. Call the getDate() method on the Date to get the days.
  2. Subtract the days.
  3. Pass the result of the subtraction to the setDate() method.

For example:

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

  return date;
}

// May 15, 2022
const date = new Date('2022-05-15T00:00:00.000Z');

const newDate = subtractDays(date, 5);

// May 10, 2022
console.log(newDate); // 2022-05-10T00:00:00.000Z

Our subtractDays() function takes a Date object and the number of days to subtract as arguments. It returns the same Date object with the days subtracted.

The Date getDate() method returns a number between 1 and 31 that represents the day of the month of the particular Date.

The Date setDate() method changes the day of the month of the Date object to the number passed as an argument.

If the days you specify would change the month or year of the Date, setDate() automatically updates the Date information to reflect this.

// April 25, 2022
const date = new Date('2022-04-25T00:00:00.000Z');

date.setDate(40);

// May 10, 2022
console.log(date); // 2022-05-10T00:00:00.000Z

console.log(date.getDate()); // 10

April has only 30 days, so passing 40 to setDate() here increments the month by one and sets the day of the month to 10.

Avoiding Side Effects

The setDate() method mutates the Date object it is called on. This introduces a side effect into our subtractDays() function. To avoid modifying the passed Date and create a pure function, make a copy of the Date and call setDate() on this copy, instead of the original.

function subtractDays(date, days) {
  // 👇 Make copy with "Date" constructor
  const dateCopy = new Date(date);

  dateCopy.setDate(dateCopy.getDate() - days);

  return dateCopy;
}

const date = new Date('2022-05-15T00:00:00.000Z');

const newDate = subtractDays(date, 5);

console.log(newDate); // 2022-05-10T00:00:00.000Z

// 👇 Original not modified
console.log(date); // 2022-05-15T00:00:00.000Z

Tip: Functions that don’t modify external state (i.e., pure functions) tend to be more predictable and easier to reason about, as they always give the same output for a particular input. This makes it a good practice to limit the number of side effects in your code.

2. date-fns subDays() function

Alternatively, we can use the subDays() function from the date-fns NPM package to quickly subtract days from a Date. It works like our pure subtractDays() function.

import { subDays } from 'date-fns';

const date = new Date('2022-05-15T00:00:00.000Z');

const newDate = subDays(date, 5);

console.log(newDate); // 2022-05-10T00:00:00.000Z

// Original not modified
console.log(date); // 2022-05-15T00:00:00.000Z
Coding Beauty Assistant logo

Try Coding Beauty AI Assistant for VS Code

Meet the new intelligent assistant: tailored to optimize your work efficiency with lightning-fast code completions, intuitive AI chat + web search, reliable human expert help, and more.

See also