The getMonth function in JavaScript retrieves the integer corresponding to the month of a given date based on local time. It is important to note that the value returned by the getMonth function begins at 0, where 0 signifies January.
Syntax
The getMonth function is expressed using the subsequent syntax:
Example
dateObj.getMonth()
Return
An integer within the range of 0 to 11 that indicates the month of the given date.
JavaScript Date getMonth method example
In this section, we will explore the getMonth method by examining several examples.
Example 1
Let's see an example to print current month.
Example
<script>
var date=new Date();
document.writeln(date.getMonth()+1);
</script>
Output:
Example 2
Let's examine an example that demonstrates how to extract and display the month from a specified date.
Example
<script>
var date=new Date("August 15, 1947 20:22:10");
document.writeln(date.getMonth()+1)
</script>
Output: