JavaScript & jQuery Tutorials

JS Home Right Arrow JS Reference Right Arrow Date Right Arrow setHours()

setHours()  Date  instance method

Description

Sets the locale specific hour of the day (0-23) for the specified date.

Syntax


Signature Description
aDate.setHours( hoursInt [, minutesInt
              [, secondsInt[, millisecondsInt ]]])
Sets the locale specific hour of the day (0-23) for the specified date.

Parameters


Parameter Description
hoursIntAn integer in the range 0-23.
minutesIntAn integer in the range 0-59.
  • If minutesInt is not specified the value returned from the getMinutes() method is used.
secondsIntAn integer in the range 0-59.
  • If secondsInt is not specified the value returned from the getSeconds() method is used.
  • If secondsInt is specified you must also specify minutesInt.
millisecondsIntAn integer in the range 0-999.
  • If millisecondsInt not specified the value returned from the getMilliseconds() method is used.
  • If millisecondsInt is specified you must also specify minutesInt and secondsInt.
For specified values outside the ranges above the setHours() method will try and set the date accordingly.

Examples

The code below shows examples of the setHours() method of Date.


// Create a Date instance for the current date and time.
var todaysDate = new Date();
alert(todaysDate);
// Set hours.
todaysDate.setHours(12);
alert(todaysDate + ' - Hours set to 12');
// Set hours, minutes and seconds.
todaysDate.setHours(9,10,11);
alert(todaysDate + ' - Hours, minutes and seconds set');

Press the button below to action the above code:

Related Tutorials

JavaScript Intermediate Tutorials - Lesson 2 - Dates and Times



  

go to home page Homepage go to top of page Top