Easy Tutorial
❮ Jsref Regexp I Prop Email Disabled ❯

JavaScript setMonth() Method

JavaScript Date Object

Example

Set the month parameter to 4 (May):

var d = new Date();
d.setMonth(4);

d output result:

var d = new Date();
d.setMonth(4);
document.write(d);

Definition and Usage

The setMonth() method is used to set the month.

Note: January is 0, December is 11.

This method can also be used to set the day of the month.


Browser Support

All major browsers support the setMonth() method.


Syntax

Parameter Values

Parameter Description
month Required. A number representing the month. The value is between 0 (January) and 11 (December): -1 is the last month of the previous year, 12 is the first month of the next year, 13 is the second month of the next year.
day Optional. A number representing the day of the month, the value is between 1 and 31 (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 number of milliseconds from midnight of 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:

var d = new Date();
d.setMonth(4, 20);
document.write(d);

Example

Set the date to the last day of the previous month:

var d = new Date();
d.setMonth(d.getMonth(), 0);

d output result:

var d = new Date();
d.setMonth(d.getMonth(), 0);
document.write(d);

JavaScript Date Object

❮ Jsref Regexp I Prop Email Disabled ❯