Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript setMonth() Method

JavaScript setMonth() Method

Date Object Reference JavaScript Date object

Example

Set the month parameter to 4 (May):

var d = new Date();

d.setMonth(4);

d Output result:

Try it »


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 certain day of the month.


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the setMonth() method.


Syntax

Date

.setMonth(

month

,

day

)

Parameter value

Parameters Description
month Required. A value representing the month.

This value is between 0 (January) and 11 (December):

  • -1 is the last month of the previous year
  • 12 is the first month of next year
  • 13 is the second month of next year
  • | | *day* | Optional. A value representing a day of the month,

    This 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 milliseconds from midnight on 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:

    Try it »

    Example

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

    var d = new Date();

    d.setMonth(d.getMonth(),0);

    d Output result:

    Try it »


    Date Object Reference JavaScript Date object