JavaScript setMonth() Method
Example
Set the month parameter to 4 (May):
var d = new Date();
d.setMonth(4);
d Output result:
Try it »
Definition and Usage
The setMonth() method is used to set the month.
Note: January is 0, December is 11.
This method can be used to set a certain day of the month.
Browser support





All major browsers support the setMonth() method.
Syntax
Date
.setMonth(
month
,
day
)
Parameter value
| Parameters | Description |
|---|---|
| month | Required. A value representing the month.
This value is between 0 (January) and 11 (December): |
This value is between 1 and 31 (in local time):
- 0 is the last day of the previous month.
- -1 is the day before the last day of the previous month.
If the current month has 31 days:
- 32 is the first day of the next month
If the current month has 30 days:
- 32 is the second day of the next month.
Return Value
| Type | Description |
|---|---|
| Number | Returns the milliseconds from midnight on January 1, 1970 to the adjusted date. Before ECMAScript standardization, this method returned nothing. |
Technical Details
| JavaScript version: | 1.0 |
|---|
More examples
Example
Set the date to May 20:
var d = new Date();
d.setMonth(4,20);
d Output result:
Try it »
Example
Set the date to the last day of the previous month:
var d = new Date();
d.setMonth(d.getMonth(),0);
d Output result:
Try it »
JavaScript Date object