JavaScript setHours() method
Example
Set the time field to 15:
var d = new Date();
d.setHours(15);
d Output result:
Try it »
Definition and Usage
The setHours() method is used to set the hours field of a specified time.
This method can also be used to set the minutes, seconds, and milliseconds.
Browser support





All major browsers support the setHours() method
Syntax
Date
.setHours(
hour,min,sec,millisec
)
Parameter value
| Parameters | Description |
|---|---|
| hour | Required.
A value for the hour, between 0 (midnight) and 23 (11 PM), in local time (same below). |
, between 0 and 59. Before ECMAScript standardization, this parameter was not supported:
- -1 is the last minute of the previous hour
- 60 is the first minute of the next hour
, between 0 and 59. Before ECMAScript standardization, this parameter was not supported:
- -1 is the last second of the previous minute
- 60 is the first second of the next minute
Between 0 and 999. Before ECMAScript standardization, this parameter was not supported:
- -1 is the last millisecond of the previous second
- 1000 is the first millisecond of the next second
Return Value
| Type | Description |
|---|---|
| Number | Milliseconds between the date object and midnight January 1 1970 |
Technical Details
| JavaScript version: | 1.0 |
|---|
More examples
Example
In this example, we set the time to 15:35:01 using the setHours() method:
var d = new Date();
d.setHours(15,35,1);
d Output result:
Try it »
Example
Set the time to 48 hours ago:
var d = new Date();
d.setHours(d.getHours()-48);
d Output result:
Try it »
JavaScript Date object