JavaScript getMonth()Method

Date 对象参考手册JavaScript Date Object

Example

Return the month:

var d = new Date();
var n = d.getMonth();

nOutput:


Try it Yourself »

Definition and Usage

The getMonth() method returns the month as a number. The return value is an integer between 0 (January) and 11 (December).

Note:January is 0, February is 1, and so on.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the getMonth() method


Syntax

Date.getMonth()

Return Value

Type Description
Number The return value is an integer between 0 (January) and 11 (December).

Technical Details

JavaScript Version: 1.0


More Examples

Example

Now, we will create an array to output the name of the month, instead of a number:

var d=new Date();
var month=new Array();
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";
var n = month[d.getMonth()];

The output of the example above is:


Try it Yourself »


Date 对象参考手册JavaScript Date Object

Other Extensions