Where dateObject is the object of date which is required to call setYear() method and the value passed to the setYear() method can be any two digit or four digit number. If yearValue is a two digit number ( i.e 45) then this method will automatically assume this value as 1945.
Description with example:
To describe the use of setYear() method we have created a simple html page into which we have created an input box. This text box takes the input as a two or four digit number which represent the year to be set.
| function setYearFunction()
var dt= new Date();
var year = document.getElementById('yearValue').value;
alert("Current date :" +dt);
alert("Date after set :"+dt);
In above lines of code, first line of the function creates a date object variable which consists of current date. In the next line we have taken the year value into a variable "year" which holds the yearValue taken as input to the input box by the user. Now we can set the year of the date object by calling method setYear() over date object "dt".
Here is the full example code of setYearExample.html as follows :
Output of the HTML page would look like as given above.
Insert any year in four digit. Suppose, as in our example we have inserted value 1900 and then click on button "Set Year". It first shows the current date before calling setYear() method in an alert message box.
After alert message it will set years value to 1900 from 2009 which can be seen in out put as above.
Recommend the tutorial