Month Conversion
Programming
Date Functions
Coding Tips
Python

Get Month name from month number

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 (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

python
1import calendar
2import datetime
3
4# Using calendar module
5print(calendar.month_name[1])   # January
6print(calendar.month_name[12])  # December
7print(calendar.month_abbr[1])   # Jan
8
9# Using datetime
10date = datetime.date(2025, 3, 1)
11print(date.strftime("%B"))      # March (full name)
12print(date.strftime("%b"))      # Mar (abbreviated)
13
14# List all month names
15print(list(calendar.month_name[1:]))
16# ['January', 'February', ..., 'December']
17
18# Locale-aware formatting
19import locale
20locale.setlocale(locale.LC_TIME, "fr_FR.UTF-8")
21print(calendar.month_name[3])  # mars (French)

JavaScript

javascript
1// Using Date object and toLocaleString
2function getMonthName(monthNumber) {
3    const date = new Date(2025, monthNumber - 1, 1); // Month is 0-indexed
4    return date.toLocaleString('en-US', { month: 'long' });
5}
6
7console.log(getMonthName(1));   // January
8console.log(getMonthName(12));  // December
9
10// Abbreviated
11function getMonthAbbr(monthNumber) {
12    return new Date(2025, monthNumber - 1, 1)
13        .toLocaleString('en-US', { month: 'short' });
14}
15
16console.log(getMonthAbbr(3));  // Mar
17
18// Locale-aware
19console.log(new Date(2025, 2, 1).toLocaleString('de-DE', { month: 'long' }));
20// März (German)
21
22// Array lookup (fastest)
23const MONTHS = [
24    '', 'January', 'February', 'March', 'April', 'May', 'June',
25    'July', 'August', 'September', 'October', 'November', 'December'
26];
27console.log(MONTHS[5]); // May

Java

java
1import java.time.Month;
2import java.time.format.TextStyle;
3import java.util.Locale;
4
5// Java 8+ — Month enum
6Month month = Month.of(3);
7System.out.println(month.getDisplayName(TextStyle.FULL, Locale.ENGLISH));  // March
8System.out.println(month.getDisplayName(TextStyle.SHORT, Locale.ENGLISH)); // Mar
9
10// Locale-aware
11System.out.println(month.getDisplayName(TextStyle.FULL, Locale.FRENCH));   // mars
12
13// All months
14for (int i = 1; i <= 12; i++) {
15    System.out.println(Month.of(i).getDisplayName(TextStyle.FULL, Locale.ENGLISH));
16}

C#

csharp
1using System;
2using System.Globalization;
3
4// Using CultureInfo
5string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(3);
6Console.WriteLine(monthName);  // March
7
8// Abbreviated
9string abbr = CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(3);
10Console.WriteLine(abbr);  // Mar
11
12// Using DateTime
13DateTime date = new DateTime(2025, 3, 1);
14Console.WriteLine(date.ToString("MMMM"));  // March
15Console.WriteLine(date.ToString("MMM"));   // Mar
16
17// Locale-aware
18CultureInfo french = new CultureInfo("fr-FR");
19Console.WriteLine(french.DateTimeFormat.GetMonthName(3));  // mars

SQL

sql
1-- MySQL
2SELECT MONTHNAME('2025-03-15');    -- March
3SELECT DATE_FORMAT('2025-03-15', '%M');  -- March
4
5-- PostgreSQL
6SELECT TO_CHAR(DATE '2025-03-15', 'Month');  -- March
7SELECT TO_CHAR(DATE '2025-03-15', 'Mon');    -- Mar
8
9-- SQL Server
10SELECT DATENAME(MONTH, '2025-03-15');  -- March
11SELECT FORMAT(CAST('2025-03-15' AS DATE), 'MMMM');  -- March

Common Pitfalls

  • Off-by-one errors with 0-indexed months: JavaScript's Date constructor uses 0-indexed months (0 = January, 11 = December), but most other languages use 1-indexed (1 = January). Passing month number 3 to JavaScript new 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 (toLocaleString in JS, TextStyle with Locale in Java, CultureInfo in C#) when the output is user-facing.
  • Calendar module indexing in Python: calendar.month_name is a 13-element array where index 0 is an empty string. Using calendar.month_name[0] returns "", not January. Index 1 is January.
  • Performance in tight loops: Creating Date/DateTime objects 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] or datetime.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

Course illustration
Course illustration

All Rights Reserved.