JavaScript > Date

setUTCHours() Method in JavaScript

How to set the hour of a date object according to the UTC time in JavaScript?


By using setUTCHours() method we can set the hour of a date object, according to the UTC time.

<p>Click the below button to set the Hour of a date object, according to UTC time.</p>
<input type="button" value="Click" onclick="myDate()" />
<p id="myId"></p>

<script>
    function myDate() {
        var a = new Date();
        a.setUTCHours(20);
        document.getElementById("myId").innerHTML = a;
    }
</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 set the hour of a date object according to UTC time, for that we are using setUTCHours()method and we have given value "20" to this method.  Onclick of the button "Click" in the HTML code fires the function myDate() in the <script> block, at the same time setHours()method sets the hour of a date object according to UTC time  as per the given value (20) and gives the output.

OUTPUT

NOTE: The output is showing as per the IST.

 Views: 3283 | Post Order: 128



Write for us






Hosting Recommendations