JavaScript Date setMonth() Method

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

Try It Yourself

Example 2

Set the month to 4 (May) and the date to 20th:

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

Try It Yourself

Example 3

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

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

Try It Yourself

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:

  • -1 will lead to the last month of the previous year
  • 12 will lead to the first month of the next year
  • 13 will lead to the second month of the next year
day

Optional. An integer representing the day of the month.

The expected value is 1-31, but other values are allowed:

  • 0 will lead to the last day of the previous month
  • -1 will lead to the day before the last day of the previous month

If a month has 31 days:

  • 32 will lead to the first day of the next month

If a month has 30 days:

  • 32 will lead to the second day of the next month

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