Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript Date setUTCHours() Method

  • Last Updated : 27 Oct, 2021

Below is the example of Date setUTCHours() method.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • Example:




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




    <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 26-24=2, 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 1 and above
    • Edge 12 and above
    • Firefox 1 and above
    • Internet Explorer 4 and above
    • Opera 4 and above
    • Safari 1 and above



    My Personal Notes arrow_drop_up
  • Recommended Articles
    Page :

    Start Your Coding Journey Now!