Method
JavaScript Date getMonth() Method
The JavaScript getMonth()
method is used to get the month of a given date according to local time. The returned value is an integer between 0 and 11, representing the months January through December.
Syntax
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
dateObj.getMonth()
[/dm_code_snippet]
Parameter Values
This method does not accept any parameters.
Return Value
Returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).
Examples
The following examples illustrate the use of the getMonth()
method.
Example 1: Get Month of Current Date
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
let today = new Date(); let month = today.getMonth(); console.log(month); // Output: 8 (September)
[/dm_code_snippet]
Example 2: Get Month of Specific Date
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
let date = new Date("April 18, 2020"); let month = date.getMonth(); console.log(month); // Output: 3 (April)
[/dm_code_snippet]
Browser Support
The getMonth()
method is supported by all major browsers.
Conclusion
The getMonth()
method is a useful way to get the month of a given date according to local time. It is supported by all major browsers and can be used in both client-side and server-side JavaScript applications.