Example 1 — Set the Seconds Only
MDN-style: change the seconds and keep the existing minutes.
const event = new Date(1975, 7, 19, 23, 15, 30);
event.setSeconds(42);
console.log(event.getSeconds()); // 42
console.log(event.getMinutes()); // 15 How It Works
- Start at 23:15:30. Month index
7is August. - Call
setSeconds(42). Only the seconds argument is provided. - Minutes stay. Minutes remain
15; ms keep their current value. - Object mutated. The same
eventnow reads seconds42.

