Date.prototype.getMonth
Returns the month (0-11) for the specified date according to local time
Syntax
date.getMonth()Return Value
An integer from 0 (January) to 11 (December)
Examples
const date = new Date('2024-06-15');
console.log(date.getMonth()); // 5 (June is month 5)const months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
const now = new Date();
console.log(months[now.getMonth()]);function daysInMonth(year: number, month: number) {
return new Date(year, month + 1, 0).getDate();
}
console.log(daysInMonth(2024, 1)); // 29 (Feb in leap year)Understanding Date.prototype.getMonth
The Date.prototype.getMonth method in JavaScript returns the month (0-11) for the specified date according to local time. It belongs to the Date object and is one of the most widely used methods for working with date values in modern JavaScript and TypeScript applications.
The method signature is date.getMonth(). When called, it returns an integer from 0 (january) to 11 (december). Understanding when and how to use getMonth() helps you write more expressive, readable code.
Common use cases for Date.prototype.getMonth include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like date-getfullyear, date-getdate, date-setmonth, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Date.prototype.getMonth is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
More Date Methods
Other methods in the Date object
Related Tools
More Date Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.