Postgres DATE off by one day in JavaScript: the fix
A Postgres date timezone bug in JavaScript filed today's sales under yesterday. Why toISOString shifts a DATE back a day, and how to fix the code and the tests.
By Hamza Ghouri
A Postgres DATE column holds a calendar day with no time and no timezone. JavaScript has no type for that, so the value usually arrives as a full Date at midnight, and the first toISOString() call can move it to the day before. This Postgres date timezone JavaScript bug hit a point of sale I built for a retail shop: the dashboard filed today's sales under yesterday. This post covers how it showed up, why it happens, the fix, and the part that took longer to see: tests that only failed in the evening.
The symptom: "Sales today" read 0
The server runs in Pakistan time (PKT, five hours ahead of UTC). After a sale, the dashboard's "Sales today" figure read 0, while the invoice count on the same screen showed the sale. Nothing was lost. The sale was in the database with the right date. It was counted against yesterday.
That mismatch is the tell. When two numbers from the same table disagree about which day a row belongs to, look at how each of them turns a date into a key.
Why is my Postgres date one day off in JavaScript?
A DATE is read into a JavaScript Date at midnight local time, and toISOString() converts that moment to UTC. In any timezone ahead of UTC, local midnight is still the previous day in UTC, so in PKT 2026-08-26 becomes 2026-08-25T19:00:00.000Z and the first ten characters name the wrong day. Read the date's local calendar parts instead, or have the driver return DATE as a plain string.
How the Postgres date timezone shift happens
node-postgres, the most common Postgres driver for Node, describes the behaviour in its data types documentation: DATE and TIMESTAMP columns are converted into the local time of the Node process, as set by process.env.TZ. A DATE of 2026-08-26 becomes a Date for 00:00 on the 26th in the server's zone.
That object is fine as long as you read it in local time. getFullYear(), getMonth() and getDate() give back the 26th. The trouble starts with toISOString(), which always renders UTC, as MDN's toISOString reference states. People reach for toISOString().slice(0, 10) because it looks like the cleanest way to get YYYY-MM-DD. On a server set to UTC it works. On a server five hours ahead, it returns the 25th.
So the code passes on a laptop set to UTC, passes in CI running in UTC, and breaks in production the moment the server's zone is ahead. Zones behind UTC are safe from this particular shift, which is why the bug can sit unnoticed in a codebase for a long time.
The fix: read the calendar parts, or keep DATE as a string
The fix I shipped was to read the date's own calendar parts in place of toISOString(). The Date already knows its day in local time. The bug came from converting it to UTC to ask.
// DATE columns arrive at local midnight, so format from local parts, never via UTC.
export function toDateKey(d: Date): string {
const y = d.getFullYear();
const m = String(d.getMonth() + 1).padStart(2, "0");
const day = String(d.getDate()).padStart(2, "0");
return `${y}-${m}-${day}`;
}
With node-postgres there is a cleaner option for new code: stop turning DATE into a Date at all. The driver lets you override the parser for a type, and returning the raw string keeps a calendar date as a calendar date:
import pg from "pg";
pg.types.setTypeParser(pg.types.builtins.DATE, (value) => value);
With that in place, 2026-08-26 stays "2026-08-26" from the database to the screen and nothing can shift it. My default on a new project is to do this and keep Date objects for real instants, stored as TIMESTAMPTZ. On an existing codebase, a global parser change alters the type of every DATE column at once, so I'd fix the call sites first and switch the parser as a separate, tested change.
Which day does a sale belong to? Ask the business
The timezone bug sat next to a business question, and both apps needed the same answer to it. This shop closes at 7pm, and anything rung up after that belongs to the next day's takings. So the day of a sale is a business rule. The till and the dashboard now both date sales by that rule, not by the clock.
Two more details made the reports line up with how the shop actually works:
- An admin can pin the till to another date. When a day's sales are entered after the fact, they belong to the day they happened, so the till accepts an explicit date in place of the clock.
- The invoice register is ordered ascending and leaves out void sales, so the day's list reads in the order the sales happened and agrees with the totals.
If your app reports "today" anywhere, write down the cutoff and the timezone it is measured in, then compute the business date in one function that every report and every write path calls. My default is to store the moment of the sale as TIMESTAMPTZ and derive the business date from it in that one place, so a change to the cutoff is a one-line change.
The flaky tests were half the story
Three tests were failing, and they turned out to be half the story. They seeded a sale at local noon, then asserted against a dashboard that buckets by PKT. Noon on the test runner's clock and the shop's day in PKT only agree for part of the day. Whenever the two clocks straddled midnight, the seeded sale and the dashboard disagreed about which day it was on. That covered most of a PKT evening, and it covered 20:58 UTC, which is when CI last ran.
So the tests were flaky by time of day, which is the most confusing kind of flaky. They pass in the morning, fail at night, and look like noise to whoever reruns them.
The fix was to seed the data in the terms the dashboard reads it: the tests now create the sale at noon on the shop's day, so the seeded row and the bucket always agree. Then I ran the whole suite under three timezones: UTC, Asia/Karachi, and a zone behind UTC. The result was 375/375 under all three.
That three-zone run is the habit I'd keep. Node reads the TZ environment variable, so TZ=Asia/Karachi npm test runs the same suite as if the server were somewhere else. A date bug that only appears in one zone will only show up in a test that runs in that zone. One run ahead of UTC and one behind catches the shift in both directions. When I change code like this I also compare the old and new output on real data, the approach from verifying a refactor by diffing old and new output.
The short version
- A Postgres DATE read into JavaScript becomes a
Dateat local midnight.toISOString()converts it to UTC and moves it back a day anywhere ahead of UTC. - Format dates from local calendar parts, or tell the driver to return DATE as a string.
- Decide what "today" means for the business, cutoff included, and compute it in one place.
- Seed test data in the same terms the code reads it, and run the suite under UTC, a zone ahead and a zone behind.
I wrote about how the same point of sale keeps working without a connection in offline-first point of sale with IndexedDB and Dexie. Dates cause a different kind of trouble in the browser, covered in React hydration error fixes for dates and locales. More of my work is on the projects page.