79116538

Date: 2024-10-23 05:54:56
Score: 0.5
Natty:
Report link

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:

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): When you use now
  • Low reputation (1):
Posted by: Vedansh Trivedi