Convert month int to month name
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
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.
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.
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.
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
0or13. - 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
calendaror JavaScriptIntl.DateTimeFormatwhen 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.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.