Opening messages at local midnight in every time zone
How we seal wishes until the right day with Intl, a stored time zone and one integer in D1 To open something at local midnight for a user, store their IANA time zone next to the date. When the record is created, turn th
How we seal wishes until the right day with Intl, a stored time zone and one integer in D1
To open something at local midnight for a user, store their IANA time zone next to the date. When the record is created, turn that date into a single UTC instant, using Intl.DateTimeFormat and checking the offset twice so daylight saving changes don't shift it. After that, "is it open yet?" is one integer comparison.
That's the whole trick. The rest of this post is why we needed it and the parts that bit us.
The bug that started it
Wish Tree lets friends hang wishes on your tree, and you open them on the day: your birthday, Christmas, or a date you pick. The wishes are sealed until then.
Our first app, a Christmas message tree, stored the opening day as a bare date. To be safe it released messages at the first midnight anywhere on Earth, which is UTC+14 (Kiribati). For someone in California, that moment comes the previous morning. People could read their "Christmas morning" messages about a day early.
So for Wish Tree the rule became: a tree opens at 00:00 on its day, in its owner's time zone.
Store the zone, not just the date
When someone makes a tree, the browser sends its time zone (Intl.DateTimeFormat().resolvedOptions().timeZone, e.g. America/Chicago). We validate it and store it with the tree:
export function validTz(tz) {
if (typeof tz !== 'string' || tz.length > 64 || !/^[A-Za-z_]+(\/[A-Za-z0-9_+\-]+)*$/.test(tz)) return false;
try {
format(tz);
return true;
} catch {
return false;
}
}
The regex keeps junk out cheaply. The try asks the runtime whether it actually knows the zone.
Turning a local date into an instant
JavaScript has no "midnight in Chicago on 2026-12-25" constructor. But Intl.DateTimeFormat can tell you what the wall clock in a zone shows at any instant. From that you get the zone's offset:
const formats = new Map();
function format(tz) {
let f = formats.get(tz);
if (!f) {
f = new Intl.DateTimeFormat('en-US', {
timeZone: tz, hourCycle: 'h23', year: 'numeric', month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit', second: '2-digit',
});
formats.set(tz, f);
}
return f;
}
/** How many ms the zone is ahead of UTC at instant `ts`. */
function offset(ts, tz) {
const p = {};
for (const x of format(tz).formatToParts(new Date(ts))) p[x.type] = x.value;
return Date.UTC(+p.year, +p.month - 1, +p.day, +p.hour % 24, +p.minute, +p.second) - Math.floor(ts / 1000) * 1000;
}
The Map matters on Workers. Building a DateTimeFormat is not free, and the same few dozen zones come up again and again.
Now midnight:
/** The instant (ms) of 00:00 on `date` (YYYY-MM-DD) in `tz`. Handles DST changes around midnight. */
export function zonedMidnight(date, tz) {
const [y, m, d] = date.split('-').map(Number);
const wall = Date.UTC(y, m - 1, d);
let t = wall - offset(wall, tz);
const again = wall - offset(t, tz);
if (again !== t) t = again;
return t;
}
The first guess uses the offset at the wrong instant (midnight UTC, not midnight local). Most days that doesn't matter. Near a daylight saving change it does, because the offset at our first guess can differ from the offset at the real local midnight. Measuring again at the guessed instant fixes it.
A few zones change their clocks at midnight itself, so on those days midnight is skipped or happens twice. Two passes give a sensible answer, but test the zones your users actually live in.
One integer, then cheap checks
We compute opens_at once, when the tree is made, and store it in D1 as an integer. Everything after that is cheap:
export function isOpen(tree, now = Date.now()) {
return now >= tree.opens_at;
}
No time zone math runs when someone opens a tree, and the database can index the column. Our every-15-minutes cron, which emails owners that their tree opened, is a plain range query on opens_at.
Small things we added on top
- Christmas has two options. Some families open presents on December 25, others on January 1. The owner picks one, and we compute the next occurrence in their zone.
- Late wishes still count. A tree keeps accepting wishes for 14 days after it opens. Someone back from vacation isn't locked out.
- "Today" lives in the owner's zone too. The date picker allows today up to a year ahead, and "today" means today where the owner is, not on the server.
What we'd tell our past selves
Never store "the day" without "where." A date with no time zone is a promise you can't keep for half your users.
If you want to see it in action, Wish Tree is at mywishtree.link. A tree dated today opens right away; date it tomorrow and it stays sealed until your midnight.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.