JavaScript Date setMonth() Method
- Previous Page setMinutes()
- Next Page setSeconds()
- Go Back to the Previous Level JavaScript Date Reference Manual
Definition and Usage
setMonth()
This method sets the month of the date object.
Note:January is 0, February is 1, and so on.
This method can also be used to set the date in the month.
Example
Example 1
Set the month to 4 (May):
var d = new Date(); d.setMonth(4);
Example 2
Set the month to 4 (May) and the date to 20th:
var d = new Date(); d.setMonth(4, 20);
Example 3
Set the date to the last day of the previous month:
var d = new Date(); d.setMonth(d.getMonth(), 0);
Syntax
Date.setMonth(month, day)
Parameter Value
Parameter | Description |
---|---|
month |
Required. An integer representing the month. The expected value is 0-11, but other values are allowed:
|
day |
Optional. An integer representing the day of the month. The expected value is 1-31, but other values are allowed:
If a month has 31 days:
If a month has 30 days:
|
Technical Details
Return Value: | A number representing the milliseconds between the date object and midnight on January 1, 1970. |
---|---|
JavaScript Version: | ECMAScript 1 |
Browser Support
Method | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
setMonth() | Support | Support | Support | Support | Support |
Related Pages
Tutorial:JavaScript Date
Tutorial:JavaScript Date Format
Tutorial:JavaScript Date Setting Methods
- Previous Page setMinutes()
- Next Page setSeconds()
- Go Back to the Previous Level JavaScript Date Reference Manual