setUTCHours(hours)
Sets the UTC date's hours.
Setting the hours to a number greater than 23 also increments the day of the month.
Note: UTC time is the same as GMT time.
setUTCHours() |
Yes | Yes | Yes | Yes | Yes |
dateObject.setUTCHours(hour,min,sec,millisec);
Parameter | Description |
---|---|
hour | Required. An integer representing the hour.
Expected values are 0-23, but other values are allowed: |
min | Optional. An integer representing the minutes.
Expected values are 0-59, but other values are allowed: |
sec | Optional. An integer representing the seconds
Expected values are 0-59, but other values are allowed: |
millisec | Optional. An integer representing the milliseconds
Expected values are 0-999, but other values are allowed: -1 will be the last millisecond of the previous second 1000 will be the first millisecond of the next second |
return a number representing the number of milliseconds between the date value and midnight January 1 1970.
var myDate = new Date();
myDate.setUTCHours(1);
console.log(myDate.toString());
The code above generates the following result.
The following code sets the time to 14:36:02 in UTC time.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from www. j a v a 2s . c om-->
var d = new Date();
d.setUTCHours(14, 36, 2);
document.getElementById("demo").innerHTML = d;
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code sets the time to 24 hours ago in UTC time.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from w w w .j a va2 s . c om-->
var d = new Date();
d.setUTCHours(d.getUTCHours() - 24);
document.getElementById("demo").innerHTML = d;
}
</script>
</body>
</html>
The code above is rendered as follows: