How to use setHours in JavaScript

In this article, I will explain use of setHours() method in JavaScript date object.
  • 1780

JavaScript setHours() method

  • setHours() method of date method is used to set the hours for the date a specified date according to local time.

  • setHours() method can also be used to set the minutes, seconds and milliseconds.

Syntax

Date.setHours(hour,min,sec,millisec)

  • Hours : Hours is required parameter. An integer is representing the hour and it's value between 0 and 23.

  • Min : Min is optional parameter. An integer is representing the minutes and it's value between 0 and 59.

  • Sec : Sec is optional parameter. An integer is representing the seconds and it's value between 0 and 59,. If you given the secondsValue parameter, you must also give the minutesValue.

  • Mili : Mili is optional parameter. A number is representing the milliseconds and it's value between 0 and 999. If you give the numMili parameter, you must also give the numMin and numSec.

Example

In the following example show to setHours() method set the hour to 15.

<!DOCTYPE html>

<html>

<body>

<h3 style="color:Teal ">JavaScript setHours() Method Example</h3>

<p id="demo">Click for display a date after changing the hours.</p>

<button onclick="myFunction()">Click</button>

<script type="text/javascript">

    function myFunction() {

        var d = new Date();

        d.setHours(15);

        var x = document.getElementById("demo");

        x.innerHTML ="Set the 15 in Current Hours : "+ d;

    }

</script>

</body>

</html>

Output1

setHours 1.jpg

Output2

setHours 2.jpg

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.