Latest web development tutorials

JavaScript setUTCHours () method

Date Object Reference JavaScript Date Object

Examples

According to Universal Time (UTC) field to set the hours of 15:

var d = new Date();
d.setUTCHours(15);

d output in local time:


try it"

Definition and Usage

setUTCHours () method is used according to Universal Time (UTC) to set the hour (0--23).

This method can be used to set the number of minutes, seconds and millimeters.

Tip: Coordinated Universal Time, also known as the unified world time, Universal Time, Coordinated Universal Time, abbreviated UTC (Universal Coordinated Time).

Note: UTC time is GMT (Greenwich Mean) time.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support setUTCHours () method.


grammar

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

Parameter Values

参数 描述
hour 必需。要给 dateObject 设置的小时字段的值。

该参数是 0 ~ 23 之间的整数:

  • -1 为前一天的最后一个小时。
  • 24 为明天的第一个小时
min 可选。要给 date Object 设置的分钟字段的值。

该参数是 0 ~ 59 之间的整数:

  • -1 为前一小时的最后一分钟
  • 60 为下一小时的第一分钟
sec 可选。要给 date Object 设置的秒字段的值。

该参数是 0 ~ 59 之间的整数:

  • -1 为前一分钟的最后一秒
  • 60 为下一分钟的第一秒
millisec 可选。要给 date Object 设置的毫秒字段的值。

该参数是 1 ~ 999 之间的整数:

  • -1 为前一秒钟的最后一毫秒数
  • 1000 为下一秒钟的第一毫秒数

return value

Type description
Number The number of milliseconds January 1, 1970 to the adjusted date.

technical details

JavaScript version: 1.3


More examples

Examples

According to Universal Time (UTC) Set the time to 15:35:01:

var d = new Date();
d.setUTCHours(15,35,1);

d in local output:


try it"

Examples

According to Universal Time (UTC) Set the time was 48 hours ago:

var d = new Date();
d.setUTCHours(d.getUTCHours()-48);

d in local output:


try it"


Date Object Reference JavaScript Date Object