When you use now.setDate(-1), you're not actually rolling back the date to the previous month. Instead, you're setting the date to the 1st day of the current month minus 1 day.
Here's how it works:
1.Initial Date: Let's say today is October 23, 2024. 2.Setting the Date: When you call now.setDate(-1), you're telling it to set the date to the 1st of October (because the Date object rolls back to the start of the month) and then subtract 1 day, which results in September 29, 2024.
Use this to get the
const now = new Date();
const lastDayOfPreviousMonth = new Date(now.getFullYear(), now.getMonth(), 0);
console.log(`Last Day of Previous Month: ${lastDayOfPreviousMonth}`);
Explanation: