JavaScript » Date » setHours

object.setHours(hoursVal [, minutesVal, secondsVal, msVal])
this value is an integer (0 thru 23) representing the hour.
an integer (0 thru 59) representing the minutes.
this value is an integer (0 thru 59) that represents the seconds. If you supply this parameter you also must supply minutesVal.
this value is a number between 0 and 999 that represents milliseconds. If this value is supplied, you must also supply minutesVal and secondsVal.

This method is used to set the hours for the supplied date according to local time.

This method is used to set the hours for the supplied date according to local time. If you do not supply the minutesVal and secondsVal and msVal arguments, JavaScript will use the values returned using the getUTCMinutes, getUTCSeconds and getMilliseconds methods. Also, if the supplied argument is outside the range expected, the setHours method will alter the other parameters accordingly (see example below).


myDate = new Date()
document.write(myDate +"<br>")
myDate.setHours(15, 100)
Fri Jul 9 13:47:32 UTC+0100 1999
Fri Jul 9 16:40:32 UTC+0100 1999

This example uses the setHours method to change the value of the myDate object and also demonstrates how this method will adjust the other parameters if a value is supplied that exceeds the expected range. In this case the minutesVal supplied is 100 which causes the hourVal value to be incremented by 1.

This is calculated thus:
100 - 60 (maximum expected value for minutes) = 40 (this increments hoursVal by one).

The result of this calculation (40) is then used for the minutesVal parameter.