Example 1 — Set the Hour Only
Change the hour and keep the existing minutes and seconds.
const event = new Date(1975, 7, 19, 23, 15, 30);
event.setHours(20);
console.log(event.getHours()); // 20
console.log(event.getMinutes()); // 15 How It Works
- Start at 23:15:30. Month index
7is August. - Call
setHours(20). Only the hour argument is provided. - Minutes stay. Because minutes were omitted, they remain
15. - Object mutated. The same
eventnow reads hour20.

