Emmanuel Akinfulubi

JavaScriptDate

Get the number of days in a month in JavaScript

Beginner · 3 min read

How many days are in February? It depends on the year: 28 most of the time, 29 in a leap year. Calendars, date pickers and "pay on the last day of the month" features all need the length of a month. In this tutorial you will write daysInMonth(year, month) in one line, and it will get February right without you writing the leap-year rule at all.

The trick is a little odd the first time you see it: you ask JavaScript for "day 0" of the next month. By the end you will know why that works. You need to know how to make a date with new Date(year, month, day) and that months count from 0, so January is 0 and December is 11.

Dates roll over instead of failing

When you give new Date a part that is out of range, it doesn't refuse. It carries the extra into the next unit, the way a car's odometer rolls from 999 to 1000. So 32 January becomes 1 February:

JavaScript

console.log(new Date(2026, 0, 32).toDateString()); // 'Sun Feb 01 2026'

It works backwards too. Day 1 is the first of the month, so day 0 is one day before the first. That is the last day of the month before:

JavaScript

console.log(new Date(2026, 2, 0).toDateString()); // 'Sat Feb 28 2026'

Month 2 is March, so day 0 of March is 28 February. JavaScript already knows how long February 2026 is, so it lands on the right last day.

getDate() reads the day number

getDate() gives the day of the month. On the last day of a month, that number is the month's length:

JavaScript

console.log(new Date(2026, 2, 0).getDate()); // 28

Here is the exercise. Try it first if you like, then read on and we will build it together.

8 How many days are in a month?

easy

Given a year and a zero-based month, return the number of days in it — and get February right in a leap year without writing the leap rule.

Stuck? Three hints, smallest first
  1. 1. Day 0 of a month is the last day of the month before it.
  2. 2. So new Date(year, month + 1, 0) lands on the last day of month.
  3. 3. getDate() on that is the answer, and it already knows about leap years.

Step 1: the one-line function

To get the length of month, go to day 0 of the month after it, and read the day:

Step 1

const daysInMonth = (year, month) => new Date(year, month + 1, 0).getDate();
console.log(daysInMonth(2026, 0)); // 31
console.log(daysInMonth(2026, 3)); // 30
console.log(daysInMonth(2026, 1)); // 28

January has 31, April (month 3) has 30, and February 2026 has 28.

Step 2: leap years for free

Step 2

console.log(daysInMonth(2024, 1)); // 29
console.log(daysInMonth(2100, 1)); // 28

2024 is a leap year, so February has 29. 2100 is not, even though it divides by 4, because century years only count if they divide by 400. You didn't write any of that rule. The Date constructor already follows it, and code you didn't write can't contain your mistakes.

Step 3: December does not need a special case

For December (month 11), month + 1 is 12. There is no month 12, but that is fine. Month 12 rolls over into January of the next year, and day 0 of that is 31 December:

Step 3

console.log(daysInMonth(2026, 11)); // 31
console.log(new Date(2026, 12, 0).toDateString()); // 'Thu Dec 31 2026'

The same rolling that makes day 0 work also handles the year change.

Step 4: the whole year

Use Array.from to call daysInMonth for each of the twelve months:

Step 4

const yearLengths = (year) => Array.from({ length: 12 }, (unused, m) => daysInMonth(year, m));
console.log(yearLengths(2026).join(' ')); // '31 28 31 30 31 30 31 31 30 31 30 31'

Array.from({ length: 12 }, fn) makes an array of 12 items, calling fn for each one. The second argument fn receives is the position, 0 to 11, which is exactly the zero-based month. The first argument is empty here, so it is named unused.

As a final check, add the lengths up:

JavaScript

const total = (year) => yearLengths(year).reduce((sum, n) => sum + n, 0);
console.log(total(2026), total(2024)); // 365 366

reduce walks the array and keeps a running total, starting from 0.

The full solution

The panel has daysInMonth, yearLengths and checks for every month, both year totals and the day 0 trick itself. Press Run to see them.

The answer

// Day 0 of the NEXT month is the last day of THIS month. The constructor does
// the leap-year arithmetic, so there is no rule to get wrong here.
const daysInMonth = (year, month) => new Date(year, month + 1, 0).getDate();

// The lengths for a whole year, January first.
const yearLengths = (year) => Array.from({ length: 12 }, (unused, m) => daysInMonth(year, m));

console.log('February 2026:', daysInMonth(2026, 1));
console.log('February 2024:', daysInMonth(2024, 1), '<- leap year');
console.log('2026:', yearLengths(2026).join(' '));
console.log('day 0 of March 2026 is', new Date(2026, 2, 0).toDateString());

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.

9 checks

test('January has 31', daysInMonth(2026, 0), 31);
test('February 2026 has 28', daysInMonth(2026, 1), 28);
test('February 2024 has 29, with no leap rule written', daysInMonth(2024, 1), 29);
test('April has 30', daysInMonth(2026, 3), 30);
test('December has 31', daysInMonth(2026, 11), 31);
test('the whole year, in order', yearLengths(2026), [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]);
test('a common year adds up to 365', yearLengths(2026).reduce((sum, n) => sum + n, 0), 365);
test('a leap year adds up to 366', yearLengths(2024).reduce((sum, n) => sum + n, 0), 366);
test('day 0 of March is the last day of February', new Date(2026, 2, 0).getDate(), 28);

Day 0 is the trick worth keeping. The Date constructor accepts out-of-range parts and rolls them, so day 0 of March means "one day before 1 March", which is the last day of February whatever length it happens to be. February 2024 comes back as 29 without a leap rule anywhere in the code, and code you did not write cannot be wrong.

The same rolling behaviour is a hazard elsewhere — exercise 19 is entirely about that — but here it does exactly the work you want.

Common mistakes

  • Using month instead of month + 1. Day 0 of month is the end of the month before, so every answer is a month out.
  • Passing a human month. daysInMonth(2026, 2) is March, not February. Months here count from 0.
  • Keeping your own table of month lengths. A table like [31, 28, 31, ...] is wrong every leap year unless you add the leap rule yourself.

Try it yourself

  1. Write lastDayOfMonth(year, month) that returns the Date itself instead of its day number.
  2. Use yearLengths to find which months of 2026 have 30 days. You should get four of them.
  3. Write daysLeftInMonth(date) that tells you how many days remain after the given date.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.