Emmanuel Akinfulubi

JavaScriptDate

Get the first and last day of a month in JavaScript

Intermediate · 4 min read

Monthly reports, billing periods and month views in a calendar all need the same two dates: the first day of the month and the last. The first is easy, since every month starts on the 1st. The last is the interesting one, because it can be the 28th, 29th, 30th or 31st.

In this tutorial you will write startOfMonth(date) and endOfMonth(date). They return new dates at midnight, and endOfMonth is right for every month, including February in a leap year and December at the end of the year. 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.

Building from parts

new Date(year, month, day) builds a date at midnight local time. If you pass in only the year, the month and the day, the hours, minutes, seconds and milliseconds are all zero. So building a fresh date from parts gives you midnight without any extra work.

Day 0 is the day before the 1st

When you give new Date a day that is out of range, it rolls over into the next or previous month. Day 0 means "one day before the 1st", which is the last day of the previous month:

JavaScript

const d = new Date(2026, 2, 0);   // day 0 of March 2026
console.log(d.getMonth(), d.getDate()); // 1 28

Month 1 is February, so this is 28 February 2026. That gives a neat recipe for the last day of any month: day 0 of the next month.

To print dates, this tutorial uses iso, which turns a date into YYYY-MM-DD text:

JavaScript

const iso = (d) => d.getFullYear() + '-' +
  String(d.getMonth() + 1).padStart(2, '0') + '-' +
  String(d.getDate()).padStart(2, '0');

Here is the exercise. If you want to try it on your own first, go ahead. Otherwise, read on and we will build it together.

35 The start and end of the month

fair

Return the first and last day of the month containing a date. The last day should be right for February in any year.

Stuck? Three hints, smallest first
  1. 1. The first is day 1 of the same month, which needs no arithmetic at all.
  2. 2. The last is day 0 of the next month.
  3. 3. Build a fresh date from the parts rather than mutating, so the time is midnight without extra work.

Step 1: the first of the month

Take the year and month from the date and ask for day 1:

Step 1

const startOfMonth = (date) => new Date(date.getFullYear(), date.getMonth(), 1);
const midJanuary = new Date(2026, 0, 15, 14, 30);
console.log(iso(startOfMonth(midJanuary))); // '2026-01-01'
console.log(startOfMonth(midJanuary).getHours(), startOfMonth(midJanuary).getMinutes()); // 0 0

The input was 14:30 on 15 January. The result is midnight on 1 January, because it was built from parts. The input itself is never touched: the function only reads it.

Step 2: the wrong turn, guessing the length

A first attempt at the last day might hard-code 31 and hope:

Step 2

const guess = new Date(2026, 1, 31);   // "31 February 2026"
console.log(iso(guess)); // '2026-03-03'

February 2026 has 28 days, so day 31 rolls three days into March. A table of month lengths would fix this, but then you also need the leap year rule for February. There is a shorter way.

Step 3: day 0 of next month

Use the trick from above: ask for day 0 of the month after:

Step 3

const endOfMonth = (date) => new Date(date.getFullYear(), date.getMonth() + 1, 0);
console.log(iso(endOfMonth(midJanuary)));            // '2026-01-31'
console.log(iso(endOfMonth(new Date(2026, 1, 10)))); // '2026-02-28'
console.log(iso(endOfMonth(new Date(2024, 1, 10)))); // '2024-02-29'

JavaScript knows how long every month is, including February in a leap year, so you don't have to.

Step 4: December

For December, getMonth() + 1 is 12, which isn't a real month. Does that break anything?

Step 4

console.log(iso(endOfMonth(new Date(2026, 11, 5)))); // '2026-12-31'

No. Month 12 rolls into January of the next year, and then day 0 steps back to 31 December of the original year. The year changes inside the calculation, but not in the answer.

Step 5: use the results

The last day's number is the length of the month, and the first day tells you which weekday the month starts on, which is what a calendar grid needs:

Step 5

console.log(endOfMonth(midJanuary).getDate());  // 31
console.log(endOfMonth(midJanuary).getMonth()); // 0
console.log(startOfMonth(midJanuary).getDay()); // 4

January has 31 days. The end of the month is still in January (month 0). And 1 January 2026 was a Thursday: getDay() gives 4.

Why not copy and change?

You could copy the date and call setDate(1) or setMonth(...) on the copy. Building from parts is better here for two reasons. You get midnight for free, with no setHours call. And you avoid a trap: calling setMonth on a copy of the 31st can overflow into the wrong month before you get a chance to fix it.

The full solution

The panel has both one-line helpers and prints January, both kinds of February and December. The checks cover each of those, plus midnight, the month of the result, the weekday of the first and the length of January. Press Run to see the output.

The answer

const iso = (d) => d.getFullYear() + '-' +
  String(d.getMonth() + 1).padStart(2, '0') + '-' +
  String(d.getDate()).padStart(2, '0');

// Built from parts, so both come back at local midnight with no setHours call.
const startOfMonth = (date) => new Date(date.getFullYear(), date.getMonth(), 1);
const endOfMonth = (date) => new Date(date.getFullYear(), date.getMonth() + 1, 0);

const midJanuary = new Date(2026, 0, 15, 14, 30);

console.log('January 2026 :', iso(startOfMonth(midJanuary)), 'to', iso(endOfMonth(midJanuary)));
console.log('February 2026:', iso(endOfMonth(new Date(2026, 1, 10))));
console.log('February 2024:', iso(endOfMonth(new Date(2024, 1, 10))), '<- leap year');
console.log('December 2026:', iso(endOfMonth(new Date(2026, 11, 5))));

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('the first of the month', iso(startOfMonth(midJanuary)), '2026-01-01');
test('the last of January', iso(endOfMonth(midJanuary)), '2026-01-31');
test('the last of February 2026', iso(endOfMonth(new Date(2026, 1, 10))), '2026-02-28');
test('and of February 2024', iso(endOfMonth(new Date(2024, 1, 10))), '2024-02-29');
test('December rolls the year in the arithmetic but not in the answer', iso(endOfMonth(new Date(2026, 11, 5))), '2026-12-31');
test('the end of the month is still in that month', endOfMonth(midJanuary).getMonth(), 0);
test('the time is midnight, because the date was built from parts', [startOfMonth(midJanuary).getHours(), startOfMonth(midJanuary).getMinutes()], [0, 0]);
test('1 January 2026 was a Thursday', startOfMonth(midJanuary).getDay(), 4);
test('and the last day number is the month length', endOfMonth(midJanuary).getDate(), 31);

new Date(year, month + 1, 0) is the whole answer for the end of the month, and it handles December — where month + 1 is 12 — by rolling into January of the next year and then stepping back one day. The result is 31 December of the original year, as the fifth assertion shows.

Building from parts rather than copying and mutating gives you midnight for free. It also sidesteps a subtle failure: if you copy a date on the 31st and call setMonth, you are back in the overflow trap from exercise 24.

Common mistakes

  • Using day 0 of the same month. new Date(y, m, 0) is the last day of the previous month. You need m + 1.
  • Hard-coding 30 or 31. Months differ, and February changes with leap years.
  • Worrying about December. Month 12 rolls into the next year correctly, so no special case is needed.

Try it yourself

  1. Write daysInMonth(date) using endOfMonth.
  2. Print the last day of every month in 2026 with a loop from 0 to 11.
  3. Write isLastDayOfMonth(date) that returns true on 31 January and 28 February 2026.

Keep practising

Newsletter

New writing, when it lands.

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