How to subtract days from a date using JavaScript?

You can subtract days from a date in JavaScript by using the setDate() and getDate() methods of the Date object. Here's an example:

// Create a new Date object with the original date var date = new Date('2022-09-01'); // Subtract 7 days from the date date.setDate(date.getDate() - 7); // Output the new date console.log(date);

In this example, the setDate() method is used to set the date of the original date object, and the getDate() method is used to get the day of the month from the original date. By subtracting the desired number of days from the original day and setting the new day with setDate(), the date object will reflect the desired subtraction.