JavaScript > Date

setUTCMilliseconds() Method in JavaScript

How to set the milliseconds according to UTC time in JavaScript?


By using setUTCMilliseconds() method we can set the milliseconds according to UTC.

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

<script>
    var a = new Date(); //* Returns the date & time as per the internet network *//
    alert(a); //* Displays the Var A result in the alert box *//

    function myDate() {
        a.setUTCMilliseconds(431);  //* Sets the Number of Milliseconds*//
        var r = a.getUTCMilliseconds();  //* Returns the setted number of milliseconds * //
         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, before the function there is variable new Date() with value a, which returns the present date & time as per the internet network and display in alert box. The document.getElementById("myId")returns the element that has Id"id=myID" in the HTML page. In the <script> block below the function myDate(), we have used setMilliseconds() method with value "431", which is used to set the milliseconds according to UTC and also we have used the getUTCMilliseconds() method, which returns the setted milliseconds according to UTC. Before displaying the exact output of setUTCMilliseconds() method, first it will display the present time and date in the alert box, after clicking the "Ok" button in the alert box, it will redirect to the HTML page which consists of "Click" button. Onclick of the button "Click" in the HTML code fires the function myDate() in the <script> block at the same time setUTCMilliseconds() method sets the milliseconds as per the given input value "431" & getUTCMilliseconds() returns setted number of milliseconds as output. 

OUTPUT

The above output displaying the var a = new Date(); result and the marked region showing the seconds in the present time, that seconds includes milliseconds from 0 to 999, to get the main {setUTCMilliseconds(431)} result we need to click the "ok" button in alert box, and then "Click" button in the HTML page.

 

The above output is showing 431, because we have given input value "431" to setUTCMilliseconds() method.

NOTE: setUTCMilliseconds() method is used to set the milliseconds as per the UTC with specified value, getUTCMilliseconds() method is used to return the number of milliseconds according to UTC time with the internet network.

 Views: 3374 | Post Order: 129



Write for us






Hosting Recommendations