JavaScript > Date

SetFullYear() Method in JavaScript

How to set the year of the date object in JavaScript?


By using setFullYear() method we can set the year of the date object.

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

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

OUTPUT

NOTE: This how to was posted on 15th october, 2015 and the value entered in setFullYear() method is 2032. So that in the output it is showing year 2032.

 Views: 3898 | Post Order: 118



Write for us






Hosting Recommendations