setHours() 方法根据本地时间为一个日期对象设置小时数,返回从1970-01-01 00:00:00 UTC 到更新后的 日期 对象实例所表示时间的毫秒数。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
语法
dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
JavaScript 1.3版本之前
dateObj.setHours(hoursValue)
参数
-
hoursValue - 一个 0 到 23 的整数,表示小时。
-
minutesValue - 一个 0 到 59 的整数,表示分钟。
-
secondsValue -
一个 0 到 59 的整数,表示秒数。如果指定了
secondsValue参数,则必须同时指定minutesValue参数。 -
msValue -
一个 0 到 999 的数字,表示微秒数,如果指定了
msValue参数,则必须同时指定minutesValue和secondsValue参数。
描述
如果不指定 minutesValue,secondsValue 和 msValue 参数,则会使用getMilliseconds() 方法的返回值。
如果有一个参数超出了合理范围,setHours 会相应地更新日期对象中的日期信息。例如,如果为 secondsValue 指定了 100,则分钟会加 1,然后秒数使用 40。
例子
例子:使用setHours方法
var theBigDay = new Date(); theBigDay.setHours(7);
规范
| 规范版本 | 规范状态 | 注解 |
|---|---|---|
| ECMAScript 1st Edition. Implemented in JavaScript 1.0 | Standard | Initial definition. |
| ECMAScript 5.1 (ECMA-262) Date.prototype.setHours |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) Date.prototype.setHours |
Standard |
浏览器兼容性
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
setHours |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support