.setHours()
Published Jul 16, 2023
Contribute to Docs
The .setHours()
method sets the hours for a specified date according to local time. It also can set minutes, seconds and milliseconds.
Syntax
myDate.setHours(hoursValue)
myDate.setHours(hoursValue, minutesValue)
myDate.setHours(hoursValue, minutesValue, secondsValue)
myDate.setHours(hoursValue, minutesValue, secondsValue, millisecondsValue)
.setHours()
takes the following parameters:
hoursValue
: (Required) takes values from0
to23
representing the hours.minutesValue
: (Optional) takes values from0
to59
representing the minutes.secondsValue
: (Optional) takes values from0
to59
representing the seconds. Must also specifyminutesValue
.millisecondsValue
: (Optional) takes values from0
to999
representing the milliseconds. Must also specifyminutesValue
andsecondsValue
.
The .setHours()
method changes the hours, minutes, seconds and milliseconds for a given date according to local time.
Example
In the example below, the myDate
variable takes the provided date and applies the .setHours()
method to set the hours, minutes, and seconds of the date:
const myDate = new Date('2023-02-05');myDate.setHours(15, 36, 45);console.log(myDate);
This results in the following output:
2023-02-05T15:36:45.000Z
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.