JavaScript > Date

SetUTCSeconds() Method in JavaScript

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


By using setUTCSeconds() method we can set the seconds of a date object according to UTC time.

<p>Click the below button to set the seconds according to UTC time.</p>
<input type="button" value="Click" onclick="myDate()" />
<p id="myId"></p>

<script>
    function myDate() {
        var a = new Date();
        a.setUTCSeconds(55); 
        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 seconds of a date object according to UTC time, for that we are using setUTCSeconds()In the <script> block var a = new Date() returns the date, day & time according to the internet network and A.setUTCSeconds(55) sets the seconds in the var a and returns the output. Onclick of the button "Click" in the HTML code fires the function myDate() in the <script> block at the same time setUTCSeconds() method sets the seconds of a date object and returns in the output.

OUTPUT

 Views: 545 | Post Order: 132



Write for us