JavaScript .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 from0to23representing the hours.minutesValue: (Optional) takes values from0to59representing the minutes.secondsValue: (Optional) takes values from0to59representing the seconds. Must also specifyminutesValue.millisecondsValue: (Optional) takes values from0to999representing the milliseconds. Must also specifyminutesValueandsecondsValue.
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.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours