JavaScript setHours()Method
Definition and Usage
The setHours() method is used to set the hour field of a specified time.
This method can 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 Values
| Parameter | Description |
|---|---|
| hour | Required. A number representing the hour, between 0 (midnight) and 23 (11 pm), in local time (same below).
|
| min | Optional. A number representing the minutes. Between 0 and 59. This parameter is not supported before ECMAScript standardization:
|
| sec | Optional. A number representing the seconds. Between 0 and 59. This parameter is not supported before ECMAScript standardization:
|
| millisec | Optional. A number representing the milliseconds. Between 0 and 999. This parameter is not supported before ECMAScript standardization:
|
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 will set the time to 15:35:01 using the setHours() method:
var d = new Date();
d.setHours(15,35,1);
d.setHours(15,35,1);
dOutput:
Try it Yourself »
Example
Set the time to 48 hours ago:
var d = new Date();
d.setHours(d.getHours()-48);
d.setHours(d.getHours()-48);
dOutput:
Try it Yourself »
Other Extensions
JavaScript Date Object