JavaScript > Date

Get hours of time in JavaScript

How to return the current hour of the day according to local time in JavaScript?


By using getHours() we can return the present hour of the day, according to the local time.

<p>Click the below button to get present Hour of the day.</p>
<input type="button" value="Click" onclick="myDate()" />
<p id="myId"></p>

<script>
    function myDate() {
        var a = new Date();
        var r = a.getHours();
        document.getElementById("myId").innerHTML = r;
    }
</script>

In the above code snippet we have given Id as "myId"to the second <p> element in the HTML code. There is a function myDate() in the<script>block which is connected to the Onclick of the HTML button. The document.getElementById("myId")returns the element that has Id"id=myID" in the HTML page. We need to return the present hour of the day, according to local time, for that we are using getHours(). Onclick of the button fires the function myDate() in the <script> block at the same time getHours() return the present hour of the day as output according to local time.

OUTPUT

NOTE: While submitting this HowTo post, the time is 3.21 PM according to INDIAN Local Time. So, in that output it was showing 15th hour, as per the time changes the value of the output changes but not exceeds 24.

 Views: 5636 | Post Order: 102



Write for us






Hosting Recommendations