JavaScript setMonth()Method
Example
Set the month parameter to 4 (May):
var d = new Date();
d.setMonth(4);
d.setMonth(4);
dOutput:
Try it Yourself »
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 day of the month.
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the setMonth() method.
Syntax
Date.setMonth(month,day)
Parameter Values
| Parameter | Description |
|---|---|
| month | Required. A number representing the month. The value is between 0 (January) and 11 (December):
|
| day | Optional. A number representing a day of the month, The value is between 1 and 31 (in local time):
If the current month has 31 days:
If the current month has 30 days:
|
Return Value
| Type | Description |
|---|---|
| Number | Returns the number of milliseconds between midnight January 1, 1970 and the adjusted date. Before ECMAScript standardization, this method returned nothing. |
Technical Details
| JavaScript Version: | 1.0 |
|---|
More Examples
Example
Set the date to the last day of the previous month:
var d = new Date();
d.setMonth(d.getMonth(),0);
d.setMonth(d.getMonth(),0);
dOutput:
Try it Yourself »
Other Extensions
JavaScript Date Object