getMonth()

ES1+

Returns the month (0-11) in the specified date according to local time.

Syntax

date.getMonth()

Return Value

number

A number (0-11) representing the month

Examples

JavaScript
const date = new Date('2024-03-15');
console.log(date.getMonth()); // 3월은 2
console.log(date.getMonth() + 1); // 실제 월
Output:
// 2 3

📌 When to Use

Use getMonth() for month-based filtering, calendar generation, monthly reports, or any feature requiring the month component.

⚠️ Common Mistakes

Forgetting that months are 0-indexed (January = 0, December = 11). Always add 1 for display.

Using getMonth() directly in month name arrays without accounting for the 0-based index.

✅ Best Practices

Create a month names array and use getMonth() as index: months[date.getMonth()].

For user-facing month display, use toLocaleString with month option for localized names.

⚡ Performance Notes

getMonth() is a direct property access with negligible overhead. For formatted month names, consider caching locale-formatted strings if called repeatedly.

🌍 Real World Example

Monthly Activity Calendar

Group activities by month and display them with localized month names.

function groupActivitiesByMonth(activities, locale = 'ko-KR') {
  const grouped = {};

  activities.forEach(activity => {
    const date = new Date(activity.date);
    const monthKey = `${date.getFullYear()}-${date.getMonth()}`;
    const monthName = date.toLocaleString(locale, { month: 'long', year: 'numeric' });

    if (!grouped[monthKey]) {
      grouped[monthKey] = { monthName, activities: [] };
    }
    grouped[monthKey].activities.push(activity);
  });

  return Object.values(grouped);
}

const activities = [
  { name: 'Meeting', date: '2024-03-15' },
  { name: 'Workshop', date: '2024-03-20' },
  { name: 'Conference', date: '2024-04-10' }
];

console.log(groupActivitiesByMonth(activities));
// [{ monthName: '2024년 3월', activities: [...] }, { monthName: '2024년 4월', activities: [...] }]

Related Methods