JavaScript > Date

SetDate() Method in JavaScript

How to set the day of the month to the date object in JavaScript?


By using setDate() method we can set the day of the month to the date object.

<p>Click the below button to set the day of the month to the date object.</p>
<input type="button" value="Click" onclick="myDate()" />
<p id="myId"></p>

<script>
    function myDate() {
        var a = new Date();
        a.setDate(18);
        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 day of the month to the date object, for that we are using setDate(). Onclick of the button "Click" in the HTML code fires the function myDate() in the <script> block, at the same time setDate() method sets the day of the month to the date object and gives the output.

OUTPUT

NOTE: This how to was posted on 15th october, 2015 and the value entered in setDate() method is 18. So the output is showing date as oct 18th.

 Views: 5081 | Post Order: 117



Write for us






Hosting Recommendations