The setHours() method sets the hour of a date object.
The setHours() method sets the hour of a date object.
This method can also be used to set the minutes, seconds and milliseconds.
Date.setHours(hour, min, sec, millisec)
Parameter | Require | Description |
---|---|---|
hour | Required. | An integer representing the hour. |
min | Optional. | An integer representing the minutes. |
sec | Optional. | An integer representing the seconds |
millisec | Optional. | An integer representing the milliseconds |
For hour value, the expected values are 0-23, but other values are allowed:
For minute value, the expected values are 0-59, but other values are allowed:
For the second value, the expected values are 0-59, but other values are allowed:
For the millisec value, the expected values are 0-999, but other values are allowed:
A Number, representing the number of milliseconds between the date object and midnight January 1 1970
Set the hour to 15:
//display a date after changing the hours. var d = new Date(); d.setHours(15);/*from w w w . java 2 s . c om*/ console.log(d); //Set the time to 15:35:01 //display a date after changing the hours, minutes, and seconds. d.setHours(15, 35, 1); console.log(d); //Set the time to 48 hours ago: d.setHours(d.getHours() - 48); console.log(d);