How to Subtract Days from a Date in JavaScript?

To subtract days from a date in JavaScript, call the getDate() method on the given date object to get the day of the month, subtract the number of days from it and then pass it to the setDate() method as an argument.

For example,

// March 20, 2023
const date = new Date('2023-03-20');

// Subtract 10 days from the date
date.setDate(date.getDate() - 10);

console.log(date);
// Output: 👉 Fri Mar 10 2023 05:30:00 GMT+0530 (India Standard Time)

What if the value of (date.getDate() - days) becomes negative or zero and you pass it to the setDate() method as an argument, will it throw an error?

No, it won’t.

Actually, If the value passed to the setDate() method is negative, zero or not in the range of 1 to 31, the setDate() method automatically adjusts the date to a valid date.

For example, if we pass the day of the month as 0 to the setDate() method, it sets the date to the last day of the previous month.

// March 31, 2023
const date = new Date('2023-03-31');

// Set day of the month as 0(adjusts accordingly)
date.setDate(0);

console.log(date);
// Output: 👉 Tue Feb 28 2023 05:30:00 GMT+0530 (India Standard Time)

Similarly, if you pass negative values to the setDate() method, it adjusts the date to previous months based on the value passed to it.

For example,

// January 31, 2023
const date = new Date('2023-01-31');

// Adjusts the date to 5 days back from last day of previous month
date.setDate(-5);

console.log(date);
// Output: 👉 Mon Dec 26 2022 05:30:00 GMT+0530 (India Standard Time)

The getDate() method on the other hand returns an integer between 1 and 31 representing the day of the month for the given date according to local time.

For example,

// March 15, 2023
const date = new Date('2023-03-15');

// Get the day of the month
const dayOfMonth = date.getDate();

console.log(dayOfMonth);
// Output: 👉 15

Subtract Days from the Current Date

To get the current date in JavaScript, you can call the Date() constructor without passing any parameters to it.

Now, if you want to get N days back date, you can first call the getDate() method to get the current day of the month and subtract N days from it and then pass it to the setDate() method.

For example,

// Today's date(22 March, 2023)
const date = new Date();

// Get 5 days back date from current date
date.setDate(date.getDate() - 5);

console.log(date);
// Output: 👉 Fri Mar 17 2023 11:24:16 GMT+0530 (India Standard Time)

Use the getTime() Method to Subtract Days from a Date

The getTime() method returns the timestamp(in milliseconds) for a given date object.

The timestamp represents the total number of seconds passed since January 1st, 1970 UTC.

That means if we want to subtract N days from a date, we can first get its timestamp(in milliseconds), subtract the total number of milliseconds in N days from the timestamp and then pass the timestamp to the Date() constructor to get the new Date object.

For example,

// March 20, 2023
const date = new Date('2023-03-20');

// Days to subtract
const N = 10;

// Get timestamp(in milliseconds) for the given date
const oldTimestamp = date.getTime();

// Subtract total milliseconds in N days from old timestamp
const newTimestamp = oldTimestamp - N * 24 * 60 * 60 * 1000;

// Get date corresponding to new timestamp
const newDate = new Date(newTimestamp);

console.log(newDate);
// Output: 👉 Fri Mar 10 2023 05:30:00 GMT+0530 (India Standard Time)

That’s all for this article. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.