METHOD:
Date::setMonth
object.setMonth(monthVal
[, dayVal])
This method is used
to set the month for the supplied date according to local time. If you
do not supply the dayVal argument, JavaScript
will use the value returned using the getDate method. Also, if
the supplied argument is outside the range expected, the setMonth
method will alter the other parameters of the date object accordingly
(see example below).
The available parameters are as follows:
monthVal - an integer (0 for January thru 11 for
December) representing the month.
dayVal - this value is an integer (1 thru 31)
that represents the day of the month.
The following code uses the setMonth 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 monthVal supplied is 13
which causes the year to be incremented by 1. This is calculated thus:
13 - 12 (maximum expected value for seconds) = 1 (this increments the
year by one).
The result of this calculation (1 = February) is then used for the monthVal
parameter.
Code:
myDate = new Date()
document.write(myDate +"<br>")
myDate.setMonth(13)
document.write(myDate)
Output:
Fri Jul 9 13:47:32 UTC+0100 1999
Wed Feb 9 13:16:40 UTC+0100 2000 |