Get Month name from month number
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Converting a month number (1-12) to its name ("January"-"December") is a common task across programming languages. Every major language provides built-in date/time libraries that handle this conversion, including locale-aware formatting for internationalization. You can also use simple array lookups for performance-critical code. The key choice is between using a date library (which handles locales and abbreviations) versus a hardcoded array (which is faster but only works for one language).
Python
JavaScript
Java
C#
SQL
Common Pitfalls
- Off-by-one errors with 0-indexed months: JavaScript's
Dateconstructor uses 0-indexed months (0 = January, 11 = December), but most other languages use 1-indexed (1 = January). Passing month number 3 to JavaScriptnew Date(2025, 3, 1)gives April, not March. Subtract 1 when constructing JavaScript dates. - Not validating input range: Passing 0 or 13 to month conversion functions causes different behavior depending on the language — some throw exceptions, some wrap around, some return empty strings. Always validate that the month number is between 1 and 12 before converting.
- Ignoring locale settings: Hardcoded English month names break for international users. Use locale-aware APIs (
toLocaleStringin JS,TextStylewithLocalein Java,CultureInfoin C#) when the output is user-facing. - Calendar module indexing in Python:
calendar.month_nameis a 13-element array where index 0 is an empty string. Usingcalendar.month_name[0]returns"", not January. Index 1 is January. - Performance in tight loops: Creating
Date/DateTimeobjects inside a loop solely to get month names is wasteful. For performance-critical code, use a static array lookup:MONTHS[monthNumber]is O(1) with no object allocation.
Summary
- Python:
calendar.month_name[n]ordatetime.strftime("%B") - JavaScript:
new Date(year, n-1, 1).toLocaleString('en', {month: 'long'}) - Java:
Month.of(n).getDisplayName(TextStyle.FULL, Locale.ENGLISH) - C#:
CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(n) - Use locale-aware APIs for internationalized applications
- Use array lookup for maximum performance
Related reading
- Get name of current script in Python
- Get node list from random walk in networkX
- Get object by id?
- Get Output From the logging Module in IPython Notebook
- Get Service selectors with K8s Python client
- Get statistics for each group such as count, mean, etc using pandas GroupBy?
- Get the current git hash in a Python script
- Get the data received in a Flask request
.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.