JavaScript Date setHours()
Description
setHours() sets the hour of a date.
setHours() can also set minutes, seconds and milliseconds.
Example 2
Set the time to 15:35:01
const d = new Date("2025-01-15");
d.setHours(15, 35, 1);
Try it Yourself »
Example 3
Set the time to 48 hours ago:
const d = new Date();
d.setHours(d.getHours() - 48);
Try it Yourself »
See Also:
Syntax
Date.setHours(hour, min, sec, millisec)
Parameters
| hour | Required. The Hours.
0 to 23, but other values are allowed:
|
| min | Optional. The Minutes.
0 to 59, but other values are allowed:
|
| sec | Optional. The seconds.
0 to 59, but other values are allowed:
|
| millisec | Optional. The milliseconds.
0 to 999, but other values are allowed:
|
Return Value
| NONE
Changes the Date object in place. |
Browser Support
setHours() is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera |