← All posts

JavaScript date off by one day? How new Date() and time zones really work

A date entered as the 16th shows up as the 15th for some users? Why new Date() and toISOString() shift dates, and how to parse, display and calculate dates in JavaScript without off-by-one bugs.

JavaScript date off by one day? How new Date() and time zones really work

Key points

  • new Date(‘2026-11-16’) is parsed as UTC and displayed in local time, so anyone in the Americas sees the 15th.
  • A calendar date (birthday, due date) and an instant (timestamp) need different handling: decide which one each field is.
  • toISOString() always converts to UTC, so never use it to get a local YYYY-MM-DD date.

It’s one of the most baffling support tickets you can get: “My booking is on the 16th, but the app says the 15th.” The developer checks, and everything looks right on their machine. The database says the 16th. And yet the customer is right. This bug almost always has the same root cause: how JavaScript parses a date string and converts it between UTC and the browser’s local time. Here’s where it comes from, how to reproduce it, and the rules that get rid of it.

The symptom and its cause

The surprising behaviour comes from a rule in the JavaScript specification. A date-only string such as '2026-11-16' is parsed as midnight UTC. But a string with a time and no offset, such as '2026-11-16T00:00', is parsed as midnight local time. Two almost identical strings, two different results.

The trouble shows up when you display the date. Methods like toLocaleDateString(), getDate() and getDay() all work in local time. Midnight UTC on 16 November is 7pm on 15 November in New York, so the displayed date slips back a day for every user west of Greenwich.

// Browser set to America/New_York (UTC-5 in November)
const d = new Date('2026-11-16');
console.log(d.toLocaleDateString('en-US')); // 11/15/2026 !

// Same thing with a time and no offset: parsed as local time
const e = new Date('2026-11-16T00:00');
console.log(e.toLocaleDateString('en-US')); // 11/16/2026

That’s why teams in the UK and Europe often miss it: midnight UTC falls on the same day there. It surfaces as soon as you have users in the Americas.

There’s a mirror-image trap for zones east of UTC. toISOString() always returns UTC, so for a date created at local midnight in Paris it returns 11pm the day before. If your code sends dates to the API with date.toISOString().slice(0, 10), it’ll send the 15th instead of the 16th for every user in Europe, Africa, Asia or Australia.

Calendar date or instant: decide for each field

The real fix is a modelling decision. There are two kinds of value that both look like dates but behave completely differently:

  • An instant: a precise moment, the same everywhere in the world. When an order was placed, when someone logged in, when an email went out. Store it in UTC and display it in the reader’s time zone.
  • A calendar date: a day on the calendar, with no time and no zone. A date of birth, an invoice due date, a public holiday, a hotel check-in date. 16 November has to stay 16 November whether you’re in Toronto or Tokyo.

JavaScript’s Date object only represents instants. As soon as you use it for a calendar date, every conversion needs care. The rule we follow: a calendar date travels and is stored as a YYYY-MM-DD string, and only becomes a Date object when you need to calculate with it, in local time, through a function you control.

// Read a 'YYYY-MM-DD' calendar date as local time, no surprises
function parseLocalDate(text) {
  const [y, m, d] = text.split('-').map(Number);
  return new Date(y, m - 1, d); // months are zero-based
}

// Write a local date as 'YYYY-MM-DD' (without going through toISOString)
function formatLocalDate(date) {
  const y = date.getFullYear();
  const m = String(date.getMonth() + 1).padStart(2, '0');
  const d = String(date.getDate()).padStart(2, '0');
  return `${y}-${m}-${d}`;
}

// The trap: in Paris (UTC+1), local midnight is 11pm UTC the day before
new Date(2026, 10, 16).toISOString(); // '2026-11-15T23:00:00.000Z'

Good news for forms: the value of an <input type=“date”> is already a YYYY-MM-DD string. Send it to your server as is, without going through new Date(). Avoid input.valueAsDate, which returns midnight UTC.

Displaying dates correctly with Intl.DateTimeFormat

For display, the Intl.DateTimeFormat API, available in every current browser, takes a timeZone option that handles both cases cleanly. For an instant, choose the display zone explicitly: the visitor’s by default, or the business’s when an appointment time has to be read in the zone where it takes place. For a date-only value that you’ve turned into a Date, the trick is to stay in UTC throughout: parsed as UTC, displayed as UTC, it no longer moves.

// An instant (an order timestamp): display it in the time zone you want
const order = new Date('2026-11-16T14:30:00Z');
new Intl.DateTimeFormat('en-US', {
  dateStyle: 'long', timeStyle: 'short', timeZone: 'Europe/Paris'
}).format(order); // 'November 16, 2026 at 3:30 PM'

// A date-only value from the API: treat it as UTC all the way through
const dueDate = new Date('2026-11-16'); // midnight UTC
new Intl.DateTimeFormat('en-US', { dateStyle: 'full', timeZone: 'UTC' })
  .format(dueDate); // 'Monday, November 16, 2026' in any visitor time zone

On one appointment-booking project, practitioners were in one time zone and some patients were travelling in another. The fix was to show appointment times in the clinic’s time zone and say so plainly (“3:30 PM, Paris time”) rather than converting silently. A patient abroad needs to know what time to turn up locally.

Calculating without falling into daylight saving traps

The second source of bugs is arithmetic. Adding a day by adding 86,400,000 milliseconds seems logical, but a local day isn’t always 24 hours long. The day clocks go forward lasts 23 hours, and the day they go back lasts 25.

// Adding days: use setDate, not milliseconds
function addDays(date, n) {
  const copy = new Date(date);
  copy.setDate(copy.getDate() + n);
  return copy;
}

// Days between two calendar dates, immune to daylight saving changes
function daysBetween(a, b) {
  const utcA = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
  const utcB = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
  return Math.round((utcB - utcA) / 86400000);
}

// Avoid: adding 24h in milliseconds. In New York, 1 November 2026 lasts 25 hours.
const wrong = new Date(new Date(2026, 10, 1).getTime() + 86400000); // still 1 November, 11pm!

setDate() works in calendar days and copes with month ends, year ends and clock changes on its own. To count days between two dates (“12 days until the due date”), converting both to midnight UTC before subtracting removes the problem, because every UTC day really is 24 hours long.

If your project handles a lot of dates, the Temporal API, designed to replace Date, offers a distinct type for each case: Temporal.PlainDate for a calendar date, Temporal.Instant for an instant, Temporal.ZonedDateTime for a time in a given zone. Browser support is still rolling out, so check compatibility or use a polyfill.

On the server and in your tests

The contract with your API should be explicit. For an instant, send and receive a full ISO string with an offset (2026-11-16T14:30:00Z or 2026-11-16T15:30:00+01:00). For a calendar date, a YYYY-MM-DD string, stored in a DATE column rather than DATETIME.

Finally, make the bug show up before your customers do. Most developers work in a single time zone, so the tests always pass. Run them in a zone west of UTC and one far to the east as well; Node.js respects the TZ environment variable.

# Run the test suite in several time zones to flush out off-by-one bugs
TZ=UTC                npm test
TZ=America/New_York   npm test
TZ=Pacific/Auckland   npm test

On our projects, these runs in CI caught date bugs in modules nobody suspected.

FAQ

Why can’t I reproduce the bug on my machine?

Because in the UK and much of Europe, midnight UTC falls on the same day as local midnight. new Date(‘YYYY-MM-DD’) only shifts the date west of Greenwich, and toISOString() only shifts it east. Change your system time zone or run your tests with TZ=America/New_York to see it.

Should every date be stored in UTC?

Instants, yes. Calendar dates, no: a date of birth or a due date should be stored as a date with no time (a DATE column in SQL, a YYYY-MM-DD string in JSON). Turning it into a UTC instant reintroduces the risk of shifting.

Dates shifting, bookings landing on the wrong day? We audit and fix the JavaScript and PHP in your applications so they behave correctly in every time zone.

Share LinkedIn Facebook X WhatsApp
</>

webtestudio

Web agency: custom WordPress sites, premium themes and SaaS software.

Discuss your project →