conversion
month names
programming
date formatting
coding tips

Convert month int to month name

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Converting a month number into a readable month name sounds simple, but there are two details that cause most bugs: whether the input is one-based or zero-based, and whether the output should be fixed English text or locale-aware formatting. Once those two choices are clear, the implementation is straightforward.

Validate the Input First

In most business code, months are one-based. That means 1 is January and 12 is December. Many date APIs, however, use zero-based month indexes internally. If you do not validate the incoming value and normalize it to the library you are using, an off-by-one error is easy to introduce.

A reliable conversion helper should:

  • reject values outside the month range
  • make the one-based versus zero-based rule explicit
  • use a standard library when localization matters

Python Example with calendar

Python already provides the mapping in the standard library.

python
1import calendar
2
3
4def month_name_from_int(value: int) -> str:
5    if not 1 <= value <= 12:
6        raise ValueError("month must be between 1 and 12")
7    return calendar.month_name[value]
8
9
10print(month_name_from_int(1))
11print(month_name_from_int(12))

calendar.month_name[0] is an empty string, so checking the range before indexing is important. If you want abbreviations, calendar.month_abbr works the same way.

python
print(calendar.month_abbr[3])

JavaScript Example with Locale Support

JavaScript adds a twist because the Date constructor uses zero-based months. If you start with a normal human month number, subtract one before creating the date.

javascript
1function monthName(value, locale = "en-US") {
2  if (value < 1 || value > 12) {
3    throw new RangeError("month must be between 1 and 12");
4  }
5
6  const date = new Date(Date.UTC(2024, value - 1, 1));
7  return new Intl.DateTimeFormat(locale, { month: "long" }).format(date);
8}
9
10console.log(monthName(4));
11console.log(monthName(4, "fr-CA"));

This is a good approach when localization matters. Instead of hardcoding English names, Intl.DateTimeFormat produces output for the requested locale.

When a Lookup Table Is Enough

If the output language is fixed and you want something obvious and fast, a small lookup table is perfectly fine.

python
1MONTHS = [
2    None,
3    "January",
4    "February",
5    "March",
6    "April",
7    "May",
8    "June",
9    "July",
10    "August",
11    "September",
12    "October",
13    "November",
14    "December",
15]
16
17
18def month_name(value: int) -> str:
19    if not 1 <= value <= 12:
20        raise ValueError("month must be between 1 and 12")
21    return MONTHS[value]

A table like this is easy to test and works well for scripts, report generation, and English-only output.

Choose the Right Output Format

Sometimes the real requirement is not just “month name.” You may need a full name, a short name, a localized label, or a format that matches another date component. That choice determines whether a lookup table is enough or whether a date-formatting API is the better fit.

In general, use a library if the application already supports multiple locales. Use a fixed mapping if the output language is stable and controlled.

Common Pitfalls

  • Forgetting that some APIs, especially in JavaScript, use zero-based months.
  • Failing to validate input and accidentally accepting 0 or 13.
  • Hardcoding English month names in an application that otherwise supports localization.
  • Passing string values from a database or form without converting them to numbers first.

Summary

  • Most human-facing month numbers are one-based, but some date APIs are zero-based.
  • Validate the range before converting the number to a month name.
  • Use Python calendar or JavaScript Intl.DateTimeFormat when library support is available.
  • A lookup table is fine when the output language is fixed and known.
  • Off-by-one errors are the main thing to watch for in month conversion code.

Course illustration
Course illustration

All Rights Reserved.