date.setHours
setHours() 方法根据本地时间为一个日期对象设置小时数,返回从1970-01-01 00:00:00 UTC 到更新后的日期对象实例所表示时间的毫秒数。
语法
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 参数。
返回值
UTC 1970年1月1日00:00:00与更新的日期之间的毫秒数。
描述
如果不指定 minutesValue,secondsValue 和 msValue 参数,则会使用getMinutes(),getSeconds() 和getMilliseconds() 方法的返回值。
如果有一个参数超出了合理范围,setHours 会相应地更新日期对象中的日期信息。例如,如果为 secondsValue 指定了 100,则分钟会加 1,然后秒数使用 40
例子
Using setHours()
var theBigDay = new Date();
theBigDay.setHours(7);规格
Specification | Status | Comment |
|---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.0. |
ECMAScript 5.1 (ECMA-262)The definition of 'Date.prototype.setHours' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Date.prototype.setHours' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262)The definition of 'Date.prototype.setHours' in that specification. | Draft | |
浏览器兼容性
Feature | Chrome | Firefox | Edge | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
Basic Support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
|---|---|---|---|---|---|---|---|
Basic Support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

