Latest web development tutorials

JavaScript setMonth () method

Date Object Reference JavaScript Date Object

Examples

Set parameters 4 month (April):

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

d output:


try it"

Definition and Usage

setMonth () method is used to set the month.

Note: Jan is 0, Dec 11

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


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support setMonth () method


grammar

Date.setMonth( month , day )

Parameter Value

参数 描述
month 必需。一个表示月份的数值。

该值介于 0(一月) ~ 11(十二月) 之间:

  • -1 为去年的最后一个月
  • 12 为明年的第一个月
  • 13 为明年的第二个月
day 可选。一个表示月的某一天的数值,

该值介于 1 ~ 31 之间(以本地时间计):

  • 0 为上个月的最后一天
  • -1 为上个月的最后一天之前的一天

如果当月有31天:

  • 32 为下个月的第一天

如果当月有30天:

  • 32 为下个月的第二天

return value

Type 描述
Number 返回1970年一月一日午夜至调整过的日期的毫秒表示。在 ECMAScript 标准化之前,该方法什么都不返回

technical details

JavaScript version: 1.0



More examples

Examples

Set the date to April 20:

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

d output:


try it"

Examples

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

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

d output:


try it"


Date Object Reference JavaScript Date Object