JavaScript Date setUTCHours() Method
Last Updated :
19 May, 2023
The date.setUTCHours() method is used to set hours into a date object according to universal time which is created using the Date() constructor.
Syntax:
DateObj.setUTCHours(hours_Value);
Parameter: This method accepts a single parameter as mentioned above and described below:
- hours_Value: This parameter holds the value of hour which is used to set in the date object created using the date() constructor.
Return Values: It returns the new i.e. updated hour which is set by the setUTCHours() method.
Note: The DateObj is a valid Date object created using the Date() constructor in which we want to set the hours.
More codes for the above method are as follows:
Below are examples of Date.setUTCHours() method.
Example 1:
Javascript
let dateobj =
new Date( 'October 13, 1996 05:35:32 GMT-3:00' );
dateobj.setUTCHours(11);
let B = dateobj.getUTCHours();
console.log(B);
|
Output:
11
Example 2: If in the Date() constructor we do not give hours while creating the Date object, still setUTCHours() method will be able to set new hour according to the universal time which is given as its parameter in the created Date object.
Javascript
let dateobj = new Date( 'October 13, 1996 GMT-3:00' );
dateobj.setUTCHours(11);
let B = dateobj.getUTCHours();
console.log(B);
|
Output:
11
Example 3: If nothing as a parameter is given in the Date() constructor, still the setUTCHours() method will be able to set hour but a month, year, and date remain current ones according to universal time.
Here 11 is the new hour, 2 is the current month i.e March, 30 is the current date and 2018 is the current year.
Javascript
let dateobj = new Date();
dateobj.setUTCHours(11);
let B = dateobj.getUTCHours();
let C = dateobj.getUTCMonth();
let D = dateobj.getUTCDate();
let E = dateobj.getUTCFullYear();
console.log(B);
console.log(C);
console.log(D);
console.log(E);
|
Output:
11
2
30
2018
Example 4: If the value of an hour as 26 is given as the parameter of the setHours() method, It will set 2 as the hour because the hour range is from 0 to 23, and hence, here 24 is subtracted because 0 to 23 is 24.
Javascript
let dateobj =
new Date( 'October 13, 1996 05:35:32' );
dateobj.setUTCHours(26);
let B = dateobj.getUTCHours();
let C = dateobj.getUTCMonth();
let D = dateobj.getUTCDate();
let E = dateobj.getUTCFullYear();
console.log(B);
console.log(C);
console.log(D);
console.log(E);
|
Output:
2
9
14
1996
We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article.
Supported Browsers: The browsers supported by the JavaScript Date setUTCHours() method are listed below:
- Google Chrome
- Internet Explorer
- Mozilla Firefox
- Opera
- Safari
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...