JavaScript setMonth()Method

Date 对象参考手册JavaScript Date Object

Example

Set the month parameter to 4 (May):

var d = new Date();
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

Internet ExplorerFirefoxOperaGoogle ChromeSafari

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):

  • -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 a day of the month,

The 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 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 May 20:

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

dOutput:


Try it Yourself »

Example

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

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

dOutput:


Try it Yourself »


Date 对象参考手册JavaScript Date Object

Other Extensions