The JavaScript getMonth() method is a built-in function that is used to get the month value from a date object. It returns an integer value ranging from 0 to 11, where 0 represents January and 11 represents December.
Date.getMonth()
Where dateObject
is a JavaScript Date
object representing the date from which you want to get the month value.
JavaScript getMonth() example
Simple example code.
<!DOCTYPE html>
<html>
<body>
<script>
var currentDate = new Date();
var currentMonth = currentDate.getMonth();
console.log("Current month: " + currentMonth);
</script>
</body>
</html>
Output:
Note that the getMonth()
method is zero-indexed, meaning January is represented by 0, February by 1, and so on. If you want to display the month with the correct index (1 to 12), you can add 1 to the result returned by getMonth()
.
var currentDate = new Date();
var currentMonth = currentDate.getMonth() + 1; // adds 1 on current date
console.log("Current month: " + currentMonth);
Do comment if you have any doubts or suggestions on this Js date method topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version