Date.prototype.setHours()

Date 实例的 setHours() 方法根据本地时间更改该日期的小时、分钟、秒和/或毫秒。

¥The setHours() method of Date instances changes the hours, minutes, seconds, and/or milliseconds for this date according to local time.

Try it

语法

¥Syntax

js
setHours(hoursValue)
setHours(hoursValue, minutesValue)
setHours(hoursValue, minutesValue, secondsValue)
setHours(hoursValue, minutesValue, secondsValue, msValue)

参数

¥Parameters

hoursValue

0 到 23 之间的整数,表示小时。

minutesValue Optional

0 到 59 之间的整数,表示分钟。

secondsValue Optional

0 到 59 之间的整数,表示秒。如果指定 secondsValue,则还必须指定 minutesValue

msValue Optional

0 到 999 之间的整数,表示毫秒。如果指定 msValue,则还必须指定 minutesValuesecondsValue

返回值

¥Return value

就地更改 Date 对象,并返回其新的 timestamp。如果参数为 NaN(或获取 coercedNaN 的其他值,例如 undefined),则日期设置为 失效日期,并返回 NaN

¥Changes the Date object in place, and returns its new timestamp. If a parameter is NaN (or other values that get coerced to NaN, such as undefined), the date is set to Invalid Date and NaN is returned.

描述

¥Description

如果不指定 minutesValuesecondsValuemsValue 参数,则使用与 getMinutes()getSeconds()getMilliseconds() 返回的值相同的值。

¥If you do not specify the minutesValue, secondsValue, and msValue parameters, the same values as what are returned by getMinutes(), getSeconds(), and getMilliseconds() are used.

如果你指定的参数超出预期范围,则 Date 对象中的其他参数和日期信息将相应更新。例如,如果为 secondsValue 指定 100,则分钟将增加 1 (minutesValue + 1),秒将使用 40。

¥If a parameter you specify is outside of the expected range, other parameters and the date information in the Date object are updated accordingly. For example, if you specify 100 for secondsValue, the minutes are incremented by 1 (minutesValue + 1), and 40 is used for seconds.

示例

¥Examples

使用 setHours()

¥Using setHours()

js
const theBigDay = new Date();
theBigDay.setHours(7);

规范

Specification
ECMAScript Language Specification
# sec-date.prototype.sethours

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看