Below is the example of Date setUTCHours() method.
- Example:
<script>// Here a date has been assigned according to// universal time while creating Date objectvardateobj =newDate('October 13, 1996 05:35:32 GMT-3:00');// New hour 11 is being set in above Date// Object with the help of setUTCHours() methoddateobj.setUTCHours(11);// New hour from above Date Object is// being extracted using getUTCHours()varB = dateobj.getUTCHours();// Printing new hourdocument.write(B);</script>chevron_rightfilter_none - Output:
11
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 accept a single parameter as mentioned above and described below:
Return Values: It returns the new i.e updated hour which is set by setUTCHours() method.
Note: The DateObj is a valid Date object created using Date() constructor in which we want to set the hours.
More codes for the above method are as follows:
Program 1: If in Date() constructor we do not give hours while creating the Date object, still setUTCHours() method will be able to set new hour according to universal time which is given as its parameter in the created Date object.
<script> // Here hour has not been assigned according to // universal time while creating Date object var 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() var B = dateobj.getUTCHours(); // Printing new hour document.write(B); </script> |
Output:
11
Program 2: If nothing as a parameter is given in Date() constructor, still 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.
<script> // Here nothing has been assigned // while creating Date object var 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() var B = dateobj.getUTCHours(); // Month from above Date Object is // being extracted using getUTCMonth() var C = dateobj.getUTCMonth(); // date from above Date Object is // being extracted using getUTCDate() var D = dateobj.getUTCDate(); // Year from above Date Object is // being extracted using getUTCFullYear() var E = dateobj.getUTCFullYear(); // Printing new Hour document.write(B + "<br />"); // Printing current month document.write(C + "<br />"); // Printing current date document.write(D + "<br />"); // Printing current year document.write(E); </script> |
Output:
11 2 30 2018
Program 3: If value of hour as 26 is given as the parameter of setHours() method, It will set 2 as the hour because hour range is form 0 to 23 and hence
, here 24 is substracted because 0 to 23 is 24.
<script> // Here nothing has been assigned // while creating Date object var 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() var B = dateobj.getUTCHours(); // Month from above Date Object is // being extracted using getUTCMonth() var C = dateobj.getUTCMonth(); // Date from above Date Object is // being extracted using getUTCDate() var D = dateobj.getUTCDate(); // Year from above Date Object is // being extracted using getUTCFullYear() var E = dateobj.getUTCFullYear(); // Printing new Hour document.write(B + "<br />"); // Printing month document.write(C + "<br />"); // Printing date document.write(D + "<br />"); // Printing year document.write(E); </script> |
Output:
2 9 14 1996
Supported Browsers: The browsers supported by JavaScript Date setUTCHours() method are listed below:
- Google Chrome
- Internet Explorer
- Mozilla Firefox
- Opera
- Safari
Recommended Posts:
- How to check the input date is equal to today's date or not using JavaScript ?
- How to convert UTC date time into local date time using JavaScript ?
- JavaScript Date toLocaleTimeString() Method
- JavaScript Date UTC() Method
- JavaScript Date getHours() Method
- JavaScript Date getMinutes() Method
- JavaScript Date getMonth() Method
- JavaScript Date getSeconds() Method
- JavaScript Date getDay() Method
- JavaScript Date getMilliseconds() Method
- JavaScript Date getFullYear() Method
- JavaScript Date getDate() Method
- JavaScript Date getUTCDate() Method
- JavaScript Date getUTCDay() Method
- JavaScript Date getUTCFullYear() Method
- JavaScript Date getUTCHours() Method
- JavaScript Date getUTCMilliseconds() Method
- JavaScript Date getUTCSeconds() Method
- JavaScript Date getUTCMinutes() Method
- JavaScript Date getUTCMonth() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


