Javascript Reference - JavaScript getMonth() Method








getMonth()
Returns the month of the date, where 0 represents January and 11 represents December.

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

Browser Support

getMonth() Yes Yes Yes Yes Yes

Syntax

dateObject.getMonth();

Parameters

None

Return Value

return a number from 0 to 11 representing the month.





Example


var myDate = new Date();
console.log(myDate.getMonth());

The code above generates the following result.

Example 2

The following code returns the name of the month defined in an array.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--  w  w w.  jav  a  2  s . c  o m-->
    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 d = new Date();
    var n = month[d.getMonth()];
    document.getElementById("demo").innerHTML = n;
}
</script>

</body>
</html>

The code above is rendered as follows: