Date.prototype.setHours
Sets the hour, and optionally minutes, seconds and milliseconds.
Available since: ES1
Syntax
date.setHours(hours, minutes, seconds, ms)Parameters
| Parameter | Type | Description |
|---|---|---|
| hours | number | The hour from 0 to 23. |
| minutes, seconds, ms | number | Optional. Minutes, seconds and milliseconds. |
Return value
numberThe new timestamp. The object itself changes.
Examples
The start of the day — midnight:
const date = new Date(2026, 2, 15, 18, 45, 30);
date.setHours(0, 0, 0, 0);
console.log(date.getHours(), date.getMinutes(), date.getSeconds());Output:
0 0 0Notes
This is handy for comparing dates without time: zero the time on both and compare getTime().
See also
- Date.prototype.getHours — Returns the hour.
- Date.prototype.setDate — Sets the day of the month — handy for adding and subtracting days.
In the tutorial
- Dates and time — The Date object in JavaScript: creating a date, reading the year, month and day, changing a date, the difference between dates and the ISO format.