The JavaScript getMonth function is one of the date methods that returns the month number of a given date according to local time. This getMonth function returns a zero-based value. It means the month number starting at 0 (January) and ending at 11 (December).
Syntax
The syntax of JavaScript getMonth() function is
dateObject.getMonth()
Return Value: The getMonth() function returns an integer value representing the month number from the given date, 0 to 11. If we pass an invalid date, it returns NaN.
- 0 = January
- 1= February
- 2 = March
- 3 = April
- ..
- 11 = December
JavaScript getMonth examples
The following set of examples helps you understand the getMonth() function.
In the following example, we use the Date() function to get the current date and time. Next, we use the getMonth function to return the month number (starting at 0 and ending at 11) from the current date and time.
let dt = new Date();
let m = dt.getMonth();
console.log(dt.toDateString())
console.log(m);
Sat Jun 06 2026
5
JavaScript getMonth() returns the previous month and fixes it
It does not return the wrong month. By default, it is designed to return 0 as the first month (January) and 11 as the last month (December). However, we see the month numbers from 1 to 12. So, if you want that kind of number, please add 1 to the getMonth() function result.
let dt = new Date();
console.log(dt.getMonth() + 1);
6
Example 2: Extracting Month from a custom Date
In the JavaScript example below, the Date(“May 12, 2023 10:09:34”) creates a date object with a custom date and time. Next, we used the getMonth() to return the month number from the custom date and time.
let dt = new Date("May 12, 2023 10:09:34");
console.log(dt.getMonth() + 1);
5
Example 3: Passing an invalid date to the getMonth function
In the following example, we constructed a date with May 32, which does not exist. So, the getMonth() function considers it an invalid date and returns NaN.
let dt = new Date("May 32, 2020");
console.log(dt.getMonth());
NaN
Example 4: Get the month number from the year
In this getMonth Function example, we extract the month number from the custom year. As you can see, the date consists of no month or day details. Here, the Date() function creates a date with January 1st as the default value of the month and day (01-01-2020). So, the getMonth() returns 0 = January.
let dt = new Date("2020 10:09:34");
console.log(dt.getMonth());
0
JavaScript getMonth name
In the following example, we declared an array of month names from January to December. Next, we use the getMonth() to get the month number from the given date and extract the corresponding month name from the array.
function monthName(date)
{
const months = ['January', 'February', 'March',
'April', 'May', 'June', 'July',
'August', 'September', 'October',
'November', 'December'];
return months[date.getMonth()];
}
let dt = new Date();
console.log(monthName(dt));
June
Why does getMonth() return the wrong month in JavaScript?
When we use the getMonth() on a custom date, we must be careful with the time zone of the local system. When converting the below parsed string date, it converts the date to the local date and time. The current time zone is Alaska, and it is 9 hours before UTC (negative time zone).
let m = new Date('2026-03-01').getMonth();
console.log(m);
1
Let me show you the date object output.
console.log(new Date('2026-03-01'));
Sat Feb 28 2026 15:00:00 GMT-0900 (Alaska Standard Time)