JavaScript setUTCHours()Method

Date 对象参考手册JavaScript Date Object

Example

Set the hour field to 15 according to UTC:

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

dOutput according to local time:


Try it Yourself »

Definition and Usage

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

This method can also be used to set minutes, seconds, and milliseconds.

Tip:Coordinated Universal Time, also known as Universal Time Coordinated, World Standard Time, International Coordinated Time, abbreviated as UTC (Universal Coordinated Time).

Note:UTC time is the same as GMT (Greenwich) time.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the setUTCHours() method.


Syntax

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

Parameter Values

Parameter Description
hour Required. The value of the hour field to be set for the dateObject.

This parameter is an integer between 0 and 23:

  • -1 is the last hour of the previous day.
  • 24 is the first hour of tomorrow.
min Optional. The value of the minute field to be set for the dateObject.

This parameter is an integer between 0 and 59:

  • -1 is the last minute of the previous hour.
  • 60 is the first minute of the next hour.
sec Optional. The value of the second field to be set for the dateObject.

This parameter is an integer between 0 and 59:

  • -1 is the last second of the previous minute.
  • 60 is the first second of the next minute.
millisec Optional. The value of the millisecond field to be set for the dateObject.

This parameter is an integer between 1 and 999:

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

Return Value

Type Description
Number The number of milliseconds between January 1, 1970 and the adjusted date.

Technical Details

JavaScript Version: 1.3


More Examples

Example

Set the time to 15:35:01 according to UTC:

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

dOutput the result according to local time:


Try it Yourself »

Example

Set the time to 48 hours ago according to UTC:

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

dOutput the result according to local time:


Try it Yourself »


Date 对象参考手册JavaScript Date Object

Other Extensions