Get the month name from a date in JavaScript
Beginner · 3 min read
Dates read better with words: "15 January 2026" is clearer than "15/01/2026", and "Jan" fits neatly on a calendar tile. JavaScript gives you the month as a number, so you have to turn it into a name yourself. In this tutorial you will write three small functions: the long month name, the short one, and a name from a month number such as 12.
This is a gentle exercise, but it has one quiet trap. A mistake here doesn't crash; it puts the word undefined on the screen. You will see exactly how that happens and how to stop it. You need to know how to make a date and how array indexes work.
Months count from 0
getMonth() returns the month as a number from 0 to 11. January is 0 and December is 11:
console.log(new Date(2026, 0, 15).getMonth()); // 0
console.log(new Date(2026, 11, 25).getMonth()); // 11
That is awkward for people, who count months from 1. But it is handy for arrays, because array indexes also start at 0. An array of twelve names, with January first, lines up with getMonth() exactly.
Two kinds of month number
In this exercise you will meet both kinds, so it helps to name them:
- The zero-based month comes from a
Date: 0 to 11. - The human month is the one people write down: 1 to 12.
Keeping them apart in your head is most of the work.
Here is the exercise. Try it yourself first, or read on and we will build it together.
4 The name of the month
easyTurn a date into its month name, long and short. Then go the other way: month name from a 1-to-12 number.
Stuck? Three hints, smallest first
- 1.
getMonth()is zero-based, so the array index and the month number differ by one. - 2. Twelve names, index 0 being January.
- 3. For a 1-to-12 number, subtract one before indexing — and reject anything outside that range.
Step 1: the array of names
const MONTH_NAMES = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December',
];
console.log(MONTH_NAMES.length); // 12
console.log(MONTH_NAMES[11]); // 'December'
Twelve names, so the last index is 11, not 12.
Step 2: the name of a date's month
Use getMonth() as the index:
const monthName = (date) => MONTH_NAMES[date.getMonth()];
console.log(monthName(new Date(2026, 0, 15))); // 'January'
console.log(monthName(new Date(2026, 11, 25))); // 'December'
No adding or subtracting is needed here, because both sides count from 0.
Step 3: the short name
Take the first three letters of the long name:
const shortMonthName = (date) => monthName(date).slice(0, 3);
console.log(shortMonthName(new Date(2026, 8, 2))); // 'Sep'
Month 8 is September. Building the short name from the long one means there is only one list to keep correct.
Step 4: from a human month number, and the trap
Now say a form gives you the month as 1 to 12. The first idea is to use it directly:
console.log(MONTH_NAMES[1]); // 'February', but month 1 is January
console.log(MONTH_NAMES[12]); // undefined
Both lines are off by one. The second is worse than it looks. Reading past the end of an array is not an error in JavaScript; you just get undefined. If you then build a sentence like 'Due in ' + name, the page says "Due in undefined", far away from where the real mistake is.
Step 5: subtract one, and refuse bad input
Subtract 1 to turn a human month into an index. Before that, check the number is a whole number from 1 to 12:
function nameOfMonthNumber(humanMonth) {
if (!Number.isInteger(humanMonth) || humanMonth < 1 || humanMonth > 12) return null;
return MONTH_NAMES[humanMonth - 1];
}
console.log(nameOfMonthNumber(1)); // 'January'
console.log(nameOfMonthNumber(12)); // 'December'
console.log(nameOfMonthNumber(13)); // null
console.log(nameOfMonthNumber(2.5)); // null
Number.isInteger is true only for whole numbers, so 2.5 and text such as '3' are turned away. || means "or": if any one of the three checks fails, the function stops.
Why return null and not undefined? undefined is also what you get when someone forgets to pass an argument at all. null says clearly "I looked, and there is no such month", so the code calling it can tell the two cases apart.
The full solution
The panel has the array, all three functions and checks for the edges, including month 13 and the missing index 12. Press Run to see them pass.
const MONTH_NAMES = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December',
];
const monthName = (date) => MONTH_NAMES[date.getMonth()];
const shortMonthName = (date) => monthName(date).slice(0, 3);
// From a human 1-to-12 number. Out-of-range returns null rather than undefined,
// so a caller can tell "no such month" from "you forgot an argument".
function nameOfMonthNumber(humanMonth) {
if (!Number.isInteger(humanMonth) || humanMonth < 1 || humanMonth > 12) return null;
return MONTH_NAMES[humanMonth - 1];
}
console.log('15/01/2026 ->', monthName(new Date(2026, 0, 15)));
console.log('25/12/2026 ->', monthName(new Date(2026, 11, 25)));
console.log('month 1 by number ->', nameOfMonthNumber(1));
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('month 0 is January', monthName(new Date(2026, 0, 15)), 'January');
test('month 11 is December', monthName(new Date(2026, 11, 25)), 'December');
test('the short form', shortMonthName(new Date(2026, 8, 2)), 'Sep');
test('twelve names', MONTH_NAMES.length, 12);
test('the last index is 11, not 12', MONTH_NAMES[11], 'December');
test('there is no index 12', MONTH_NAMES[12], undefined);
test('month number 1 is January', nameOfMonthNumber(1), 'January');
test('month number 12 is December', nameOfMonthNumber(12), 'December');
test('month number 13 is refused', nameOfMonthNumber(13), null);
MONTH_NAMES[12] is undefined, not an error, so an off-by-one here leaks a literal undefined into whatever you were building rather than failing where the mistake is. That is the case worth an assertion.
nameOfMonthNumber returns null for a bad month instead of undefined, so the caller can distinguish a rejected value from a missing one.
Common mistakes
- Indexing with a human month.
MONTH_NAMES[1]is February. Subtract 1 first. - Expecting an error past the end.
MONTH_NAMES[12]isundefined, silently. Check the range yourself. - Adding 1 to
getMonth()before indexing. That shifts every name by a month. Only add 1 when you show the number to a person.
Try it yourself
- Write
monthNumberOfName('March')that returns3. (Hint:indexOf.) - Make
nameOfMonthNumberaccept text like'3'by converting it withNumberfirst. Does'3.0'pass? - Print a line like
15 January 2026for any date, usinggetDate,monthNameandgetFullYear.