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
// Here a date has been assigned according to // universal time while creating Date object let dateobj = new Date( 'October 13, 1996 05:35:32 GMT-3:00' ); // New hour 11 is being set in above Date // Object with the help of setUTCHours() method dateobj.setUTCHours(11); // New hour from above Date Object is // being extracted using getUTCHours() let B = dateobj.getUTCHours(); // Printing new hour 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
// Here hour has not been assigned according to // universal time while creating Date object let dateobj = new Date( 'October 13, 1996 GMT-3:00' ); // New hour 11 is being set in above Date // Object with the help of setUTCHours() method dateobj.setUTCHours(11); // New hour from above Date Object is // being extracted using getUTCHours() let B = dateobj.getUTCHours(); // Printing new hour 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
// Here nothing has been assigned // while creating Date object let dateobj = new Date(); // New hour 11 is being set in above Date // Object with the help of setUTCHours() method dateobj.setUTCHours(11); // Hour from above Date Object is // being extracted using getUTCHours() let B = dateobj.getUTCHours(); // Month from above Date Object is // being extracted using getUTCMonth() let C = dateobj.getUTCMonth(); // date from above Date Object is // being extracted using getUTCDate() let D = dateobj.getUTCDate(); // Year from above Date Object is // being extracted using getUTCFullYear() let E = dateobj.getUTCFullYear(); // Printing new Hour console.log(B); // Printing current month console.log(C); // Printing current date console.log(D); // Printing current year 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
// Here nothing has been assigned // while creating Date object let dateobj = new Date( 'October 13, 1996 05:35:32' ); // New hour 26 is being set in above Date // Object with the help of setUTCHours() method dateobj.setUTCHours(26); // Hour from above Date Object is // being extracted using getUTCHours() let B = dateobj.getUTCHours(); // Month from above Date Object is // being extracted using getUTCMonth() let C = dateobj.getUTCMonth(); // Date from above Date Object is // being extracted using getUTCDate() let D = dateobj.getUTCDate(); // Year from above Date Object is // being extracted using getUTCFullYear() let E = dateobj.getUTCFullYear(); // Printing new Hour console.log(B); // Printing month console.log(C); // Printing date console.log(D); // Printing year 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.