JavaScript Date setUTCHours() Method

Below is the example of Date setUTCHours() method.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
       // Here a date has been assigned according to
       // universal time while creating Date object
       var 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()
       var B = dateobj.getUTCHours();
      
       // Printing new hour
       document.write(B);
    </script>

    chevron_right

    
    

  • 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:

  • hours_Value: This parameter holds the value of hour which is used to set in date object created using the date() constructor.
  • 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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

    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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

    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 26-24=2, here 24 is substracted because 0 to 23 is 24.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

    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

    full-stack-img




    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    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.


    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.