JavaScript Date setHours() Method
- Previous Page setFullYear()
- Next Page setMilliseconds()
- Go to the Previous Level JavaScript Date Reference Manual
Definition and Usage
setHours()
Method to set the hour of the date object.
This method can also be used to set minutes, seconds, and milliseconds.
Example
Example 1
Set the hour to 15:
var d = new Date(); d.setHours(15);
Example 2
Example
Set the time to 15:35:01
var d = new Date(); d.setHours(15, 35, 1);
Example 3
Set the time to 48 hours ago:
var d = new Date(); d.setHours(d.getHours() - 48);
Syntax
Date.setHours(hour, min, sec, millisec)
Parameter Value
Parameter | Description |
---|---|
hour |
Required. Represents the integer of hours. The expected value is 0-23, but other values are allowed:
|
min |
Optional. Represents the integer of minutes. The expected value is 0-59, but other values are allowed:
|
sec |
Optional. Represents the integer of seconds. The expected value is 0-59, but other values are allowed:
|
millisec |
Optional. Represents the integer of milliseconds. The expected value is 0-999, but other values are allowed:
|
Technical Details
Return Value: | A number representing the milliseconds between the date object and midnight on January 1, 1970. |
---|---|
JavaScript Version: | ECMAScript 1 |
Browser Support
Method | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
setHours() | Support | Support | Support | Support | Support |
Related Pages
Tutorial:JavaScript Date
Tutorial:JavaScript Date Format
Tutorial:JavaScript Date Setting Methods
- Previous Page setFullYear()
- Next Page setMilliseconds()
- Go to the Previous Level JavaScript Date Reference Manual