目录

JavaScript Date setHours()

示例1

const d = new Date();
d.setHours(15);
亲自试一试 »

描述

setHours()设置日期的小时。

setHours()还可以设置分、秒和毫秒。

示例2

将时间设置为 15:35:01

const d = new Date();
d.setHours(15, 35, 1);
亲自试一试 »

示例3

将时间设置为48小时:

const d = new Date();
d.setHours(d.getHours() - 48);
亲自试一试 »

浏览器支持

setHours()是 ECMAScript1 (ES1) 功能。

所有浏览器均完全支持 ES1 (JavaScript 1997):

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes


语法

Date.setHours( hour, min, sec, millisec)

参数

hour Required. The Hours.

0 to 23, but other values are allowed:

  •  -1 gives the last hour of the previous day
  • 24 gives the first hour of the next day
min Optional. The Minutes.

0 to 59, but other values are allowed:

  •  -1 gives the last minute of the previous hour
  • 60 gives the first minute of the next hour
sec Optional. The seconds.

0 to 59, but other values are allowed:

  •  -1 gives the last second of the previous minute
  • 60 gives the first second of the next minute
millisec Optional. The milliseconds.

0 to 999, but other values are allowed:

  • -1 gives the last millisecond of the previous second
  • 1000 gives the first millisecond of the next second

返回值

一个号码。

该日期与 1970 年 1 月 1 日 00:00:00 UTC 之间的毫秒数。