JavaScript > Date

GetUTCHours() Method in JavaScript

How to return the hour, according to UTC in JavaScript?


By using getUTCHours() we can return the hour, according to UTC.

<p>Click the below button to display the present hour as per the UTC.</p>
<input type="button" value="Click" onclick="myDate()" />
<p id="myId"></p>

<script>
    function myDate() {
        var a = new Date();
        var r = a.getUTCHours();
        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 as per the UTC, for that we are using getUTCHours(). Onclick of the button "Click" in the HTML code fires the function myDate() in the <script> block at the same time getUTCHours() return the present hour as output according to UTC.

OUTPUT

NOTE: This How To was posted when the UTC time is 8.52 AM, that's why in the output it is showing 8th hour according to UTC. 


 Views: 553 | Post Order: 112



Write for us