Below is the example of Date setUTCHours() method.
// Here a date has been assigned according to
// universal time while creating Date object
'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
// New hour from above Date Object is
// being extracted using getUTCHours()
B = dateobj.getUTCHours();
// Printing new hour
The date.setUTCHours() method is used to set hours into a date object according to universal time which is created using the Date() constructor.
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.
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.
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.
2 9 14 1996
- Google Chrome
- Internet Explorer
- Mozilla Firefox
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.