All articles

Last day of the month in JavaScript without timezone bugs

To get the last day of a month in JavaScript without the browser's time zone getting involved, use new Date(Date.UTC(year, month, 0)).getUTCDate(), where month is the 1-based month number, and build the YYYY-MM-DD string yourself. The common version, new Date(year, month, 0), creates local midnight in the viewer's time zone. Format that instant in any other zone and it can land on the day before, so a month query quietly loses its last day for some visitors.

Where this came up

I was working on a Next.js calendar page that loads a month of events from a Drupal back end. The client component takes a date from the URL, slices off the year and month, and asks the API for everything between the first and last day of that month. The first day is easy: it is the year-month string with -01 appended. The last day needs a calculation, and that is where the bug lived.

The original code worked out the last day with the usual Date trick and then formatted it in the site's own time zone, with formatInTimeZone from date-fns-tz. The intent was sensible: the events belong to one place, so dates should follow that place's calendar. The effect was the opposite.

Why new Date(year, month, 0) depends on the viewer

new Date(year, monthIndex, day) interprets its arguments in the runtime's local time zone. Day 0 of a month rolls back to the last day of the previous month, which is why new Date(2026, 11, 0) (month index 11 is December) gives 30 November. That part is fine.

The problem is what the value actually is. It is not "30 November". It is one instant: midnight at the start of 30 November in the viewer's time zone. A Date has no idea it was meant as a calendar date.

Now format that instant in a different zone. Say the site runs on America/Chicago and the visitor is in London:

// Browser time zone: Europe/London
import { formatInTimeZone } from "date-fns-tz";

const lastDay = new Date(2026, 11, 0);
// 2026-11-30T00:00:00 in London, which is 2026-11-29T18:00 in Chicago

formatInTimeZone(lastDay, "America/Chicago", "yyyy-MM-dd");
// "2026-11-29"

Midnight in London is the previous evening in Chicago, so the formatted date is a day early. The month query then ends on the 29th and the events on the 30th fall outside it. For a visitor west of the site's zone, local midnight is already the same calendar day in the site's zone, so they see the right result. For visitors in the site's own zone it also works, which is why this kind of bug survives local testing.

Why formatting in the site's time zone didn't help

Converting to the site's zone is the right move when you start from a real instant, such as an event's start timestamp. It is the wrong move for a value that was never an instant. The last day of a month is pure calendar arithmetic: it depends only on the year and the month number, not on where anyone is standing.

Once a date-only value has been turned into an instant, any time zone conversion afterwards can only move it. The fix is to never create the instant in the first place, or to create it in a zone where nothing will move it.

How to compute it without a time zone

Date.UTC takes the same arguments as the local constructor but interprets them as UTC and returns a timestamp. Read the result back with the UTC getters and the viewer's zone never enters the calculation:

// "2026-02" -> "2026-02-28"
export function lastDayOfMonth(yearMonth) {
  const [year, month] = yearMonth.split("-").map(Number);
  // month is 1-based here, so day 0 of the next month index is this month's last day
  const day = new Date(Date.UTC(year, month, 0)).getUTCDate();
  return `${yearMonth}-${String(day).padStart(2, "0")}`;
}

lastDayOfMonth("2026-11"); // "2026-11-30"
lastDayOfMonth("2028-02"); // "2028-02-29"

The trick with the month number is worth spelling out. JavaScript month indexes are 0-based, so a 1-based month number m used as an index points at the next month, and day 0 of that rolls back to the last day of month m. Leap years come for free because the Date arithmetic already knows about them.

The fix was small: replace the local constructor and the zone formatting with this calculation, and build the string from the year-month the page already had. Both ends of the range are now plain strings derived from the URL, so the request is identical for every visitor.

Testing it the way the bug hides

Because the bug depends on the runtime's zone, a unit test that runs in your own zone proves little. In Node.js you can pin the zone for a test run with the TZ environment variable, and run the same suite under a zone on each side of the site's zone:

TZ=Pacific/Kiritimati node --test
TZ=Pacific/Pago_Pago node --test

Those two are about as far apart as real zones get, one ahead of UTC by fourteen hours and the other behind it by eleven. A date-only helper that returns the same string under both is not leaking the local zone. For browser tests, Playwright's timezoneId option does the same for a page.

What about Temporal?

The Temporal API models this properly: a Temporal.PlainYearMonth is a calendar month with no time and no zone, and it has a daysInMonth property. Where Temporal is available in every runtime you support, that is the clearer choice, because the type itself makes it impossible to confuse a calendar date with an instant. Until then, Date.UTC with the UTC getters is the dependable way to do calendar arithmetic with Date.

Key takeaways

  • new Date(year, monthIndex, day) creates local midnight in the viewer's time zone, not a calendar date.
  • Formatting that value in another time zone can move it to the previous day for anyone whose browser is ahead of that zone.
  • The last day of a month is calendar arithmetic, so compute it with Date.UTC(year, month, 0) and getUTCDate() and build the string yourself.
  • Keep date-only values as strings end to end, and convert time zones only for real timestamps.
  • Run date tests under time zones on both sides of UTC, with TZ in Node.js or timezoneId in Playwright, because the bug is invisible in your own zone.

Written by Artem Shymko, a senior software engineer in Belgium working on Drupal, .NET and LLM features. Get in touch if you are working on something similar.