How to subtract or add days from a Date in JavaScript?

If you have a date in JavaScript and need to subtract (or add) a number of days to that date, you will want to use JavaScript’s built in setDate() method. This will set the day of the month for a given date.

NOTE:
setDate() is based on local time. If you want to work with UTC time then use setUTCDate()

So, if you wanted to set the day of the month to be the 15th of the current month you would start with today’s date and change the date like so:

var d = new Date();
d.setDate(15);
// Sun Aug 15 2021 16:49:25 GMT-0500 (Central Daylight Time)

Using this approach combined with the getDate() method (which returns the current month’s date for a given date), you can substract or add days. If you want to get the date which is Today – 5 days then you could use:

var d = new Date();
d.setDate(d.getDate()-5);
// Tue Aug 10 2021 16:56:31 GMT-0500 (Central Daylight Time)

More Information:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate