The setMonth() method sets the month of a date object.
The setMonth() method sets the month of a date object.
January is 0, February is 1, and so on.
This method can also be used to set the day of the month.
Date.setMonth(month,day)
Parameter | Require | Description |
---|---|---|
month | Required. | An integer representing the month |
day | Optional. | An integer representing the day of month |
For month value, the expected values are 0-11, but other values are allowed:
For the day value, the expected values are 1-31, but other values are allowed:
A Number, representing the number of milliseconds between the date object and midnight January 1 1970
Set the month to 4 (May):
//display the date after changing the month. var d = new Date(); d.setMonth(4);//w w w.j a v a 2 s . c o m console.log(d); //Set the month to 4 (May) and the day to the 20th: d.setMonth(4, 20); console.log(d); //Set the date to be the last day of last month: d.setMonth(d.getMonth(), 0); console.log(d);