JavaScript | date.setHours() Function

The date.setHours() is an inbuilt function in JavaScript which is used to set hours into a date object which is created using the Date() constructor.

Syntax:

DateObj.setHours(hours_Value)

DateObj is a valid Date object created using Date() constructor in which we want to set the hours.

Parameter: Here parameter hours_Value is the value of hour which is used to set in Date() constructor.

Return Value: It returns the new date with updated hour which is set by setHours() function.



Below program illustrate the setHours() function:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Here a date has been assigned
// while creating Date object
var dateobj = new Date('October 13, 1996 05:35:32');
  
// new hour 11 is being set in above Date
// Object with the help of setHours() function
dateobj.setHours(11);
  
// new hour from above Date Object is
// being extracted using getHours()
var B = dateobj.getHours();
  
// Printing new hour
console.log(B);

chevron_right


Output:

11

Errors and Exceptions

  • Code #1: If in Date() constructor we do not give hour while creating the Date object, still setHours() function will set the new hour which is given as its parameter.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Here hour has not been assigned
    // while creating Date object
    var dateobj = new Date('October 13, 1996');
      
    // new hour 11 is being set in above Date
    // Object with the help of setHours() function
    dateobj.setHours(11);
      
    // new hour from above Date Object is
    // being extracted using getHours()
    var B = dateobj.getHours();
      
    // Printing new hour
    console.log(B);

    chevron_right

    
    

    Output:

    11
  • Code #2: If nothing as parameter is given in the Date() constructor, still setHours() function set hour but month, year and date will be current month, year and date.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 setHours() function
    dateobj.setHours(11);
      
    // hour from above Date Object is
    // being extracted using getHours()
    var B = dateobj.getHours();
      
    // month from above Date Object is
    // being extracted using getMonth()
    var C = dateobj.getMonth();
      
    // date from above Date Object is
    // being extracted using getDate()
    var D = dateobj.getDate();
      
    // year from above Date Object is
    // being extracted using getFullYear()
    var E = dateobj.getFullYear();
      
    // Printing new Hour
    console.log(B);
      
    // Printing current month
    console.log(C);
      
    // Printing current date
    console.log(D);
      
    // Printing current year
    console.log(E);

    chevron_right

    
    

    Output:

    11
    2
    30
    2018
    

    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.

  • Code #3: If value of hour as 26 is given in the parameter of setHours() function, It will set 2 as the hour because hour range is form 0 to 23 and 26%24=2.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 setHours() function
    dateobj.setHours(26);
      
    // hour from above Date Object is
    // being extracted using getHours()
    var B = dateobj.getHours();
      
    // month from above Date Object is
    // being extracted using getMonth()
    var C = dateobj.getMonth();
      
    // date from above Date Object is
    // being extracted using getDate()
    var D = dateobj.getDate();
      
    // year from above Date Object is
    // being extracted using getFullYear()
    var E = dateobj.getFullYear();
      
    // Printing new Hour
    console.log(B);
      
    // Printing month
    console.log(C);
      
    // Printing date
    console.log(D);
      
    // Printing year
    console.log(E);

    chevron_right

    
    

    Output:

    2
    9
    14
    1996
    

    Here 2 is the new hour, 9 is the month i.e october, 14 is the date and year is 1996.Here as we see that 13 was the original date but output becomes 14 because 26 hours given as the parameter of the function converted to 2 hours of next day that is why date incremented by 1 i.e, from 13 to 14.



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.