METHOD:
Date::setFullYear
object.setFullYear(yearVal [, monthVal, dayVal])
This method is used
to set the full year for the supplied date according to local time. If
you do not supply the monthVal and dayVal
arguments, JavaScript will use the values returned using the getMonth
and getDay methods. Also, if the supplied argument is outside the
range expected, the setFullYear method will alter the other
parameters accordingly (see example below).
The available parameters are as follows:
yearVal - this value is an integer representing
the year, e.g. 1999
monthVal - an integer representing the month (0
for January thru 11 for December)
dayVal - this value is an integer that represents
the day of the month (1 thru 31).
If you supply this parameter you also must supply the monthVal.
The following code uses the setFullYear method to change the value
of the myDate object and also demonstrates how this method will adjust
the other parameters if a value is supplied that exceeds the expected
range. In this case the dayVal supplied is 35
which causes the monthVal value to be incremented
by 2. This is calculated thus:
35 - 31(maximum expected value for month) = 4 (this increments monthVal
by one).
The result is that setFullYear method uses 4 for for the dayVal
and increments the monthVal by one, from 8 to 9.
Code:
myDate = new Date()
document.write(myDate +"<br>")
myDate.setFullYear(1999, 08, 35)
document.write(myDate)
Output:
Fri Jul 9 12:32:32 UTC+0100 1999
Sat Sep 4 12:32:32 UTC+0100 1999 |