How to get the current month in JavaScript
Getting the current month is crucial for date filtering, monthly reports, and seasonal functionality in web applications.
As the creator of CoreUI, a widely used open-source UI library, I’ve implemented month extraction in numerous calendar components, analytics dashboards, and date picker widgets.
From my expertise, the most important consideration is understanding that getMonth()
returns zero-based values (0-11).
This method requires careful handling to match standard month numbering expectations in user interfaces.
Use getMonth()
method to get the current month, remembering it returns 0-11.
const currentMonth = new Date().getMonth() + 1
The getMonth()
method returns the month as a zero-based index where January is 0, February is 1, and so on through December as 11. For standard month numbering (1-12), add 1 to the result: getMonth() + 1
. This zero-based system is consistent with JavaScript’s Date constructor but differs from human-readable month numbers. When displaying months to users, either add 1 for numeric display or use month name arrays for text representation.
Best Practice Note:
This is the same approach we use in CoreUI components for month-based filtering and calendar navigation. Always remember the zero-based indexing - a common source of off-by-one errors is forgetting to add 1 when displaying month numbers to users.