Now you can listen to this article with the Online Text Reader


JavaScript setHours Method





The setHours method sets the hour value of the point in time stored in a Date object.

Besides, it can also be used to set the minute, the second, and the millisecond values, and to increase or decrease the point in time.

The value returned by this method is the quantity of milliseconds passed since January 1st 1970 until the point in time stored in the Date object once it has been modified.


Syntax

The setHours method has four overloads:

myDate.setHours( hours )

myDate.setHours( hours, minutes )

myDate.setHours( hours, minutes, seconds )

myDate.setHours( hours, minutes, seconds, milliseconds )

With:
  • hours being a required parameter that consists of an integer number that, when it is between 0 and 23, changes only the hour value of the point in time stored in a Date object. Any value outside that range increases or decreases the point in time of a Date object in a quantity equal to the range's limit being exceeded plus the remaining hours.
  • minutes being an optional parameter that consists of an integer number that, when it is between 0 and 59, changes only the minute value of the point in time stored in a Date object. Any value outside that range increases or decreases the point in time of a Date object in a quantity equal to the range's limit being exceeded plus the remaining minutes.
  • seconds being an optional parameter that consists of an integer number that, when it is between 0 and 59, changes only the seconds value of the point in time stored in a Date object. Any value outside that range increases or decreases the point in time of a Date object in a quantity equal to the range's limit being exceeded plus the remaining seconds.
  • milliseconds being an optional parameter that consists of an integer number that, when it is between 0 and 999, changes only the milliseconds value of the point in time stored in a Date object. Any value outside that range increases or decreases the point in time of a Date object in a quantity equal to the range's limit being exceeded plus the remaining milliseconds.
  • myDate being an object based on the Date object.


Example

The following code uses the setHours method to increase the point in time stored in the Date object in two hours:

<script type="text/javascript">

var currentDate = new Date();

var outcome = "Current date is: <br /><br />";

outcome += currentDate + "<br /><br />";

var newHour = currentDate.getHours() + 2;

currentDate.setHours(newHour);

outcome += "After increasing the hour value in two hours, the resulting date is:<br/><br/>";

outcome += currentDate;

document.write(outcome);

</script>


Technical Details

The setHours method belongs to the Date object, it is available since the version 1.0 of JavaScript, and it is supported in all major browsers.





JavaScript Manual >> Date Object >> setHours Method





© Copyright 2010-2018 ROM Cartridge



Books to Read and Listen to Online