Example 1 — Set the Month Only
Change the month index and keep the existing day of the month.
const event = new Date(1975, 7, 19, 23, 15, 30); // Aug 19
event.setMonth(3); // April
console.log(event.getMonth()); // 3
console.log(event.getDate()); // 19 How It Works
- Start in August. Month index
7, day19. - Call
setMonth(3). Index3means April. - Day stays. Because no day argument was passed, day remains
19. - Object mutated. The same
eventnow reads month3.

