The setUTCHours() method sets the hour of a date object, according to the UTC time.
The setUTCHours() method sets the hour of a date object, according to the UTC time.
You can use this method to set the minutes, seconds and milliseconds.
UTC time is the same as GMT time.
Date.setUTCHours(hour,min,sec,millisec)
Parameter | Require | Description |
---|---|---|
hour | Required. | An integer representing the hour. |
min | Optional. | An integer representing the minutes. |
sec | Optional. | An integer representing the seconds |
millisec | Optional. | An integer representing the milliseconds |
For the hour value, the expected values are 0-23, but other values are allowed:
For the minute value, the expected values are 0-59, but other values are allowed:
For the second value, the expected values are 0-59, but other values are allowed:
For the millisec value, the expected values are 0-999, but other values are allowed:
A Number, representing the number of milliseconds between the date object and midnight January 1 1970
Set the hour to 15, according to UTC time:
//display a date after changing the hours. var d = new Date(); d.setUTCHours(15);// w w w. j av a 2s. c o m console.log(d); //Set the time to 15:35:01 UTC time //display a date after changing the hours, minutes, and seconds. var d = new Date(); d.setUTCHours(15, 35, 1); console.log(d); //Set the time to 48 hours ago, using UTC methods: //display a date after changing it to 48 hours ago. var d = new Date(); d.setUTCHours(d.getUTCHours() - 48); console.log(d);