JavaScript > Date

Get seconds of time in JavaScript

How to return the seconds of local time in JavaScript?


By using getSeconds() method we can return the seconds, according to lical time.

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

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

OUTPUT

 Views: 714 | Post Order: 106



Write for us