brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: Temporal.PlainDate does date math without the time zone bugs of Date

Javascript
~/tips/tip-js-temporal-plaindate-math

const start = Temporal.PlainDate.from("2026-01-31");

Add months and count days between calendar dates, without midnight and DST surprises.

Most Date bugs come from a calendar date being stored as a moment in time, so a time zone or summer time shifts it to the day before. Temporal.PlainDate is just a date, with no time and no zone.

const start = Temporal.PlainDate.from("2026-01-31");

start.add({ months: 1 }).toString();       // "2026-02-28"
start.until("2026-12-25", { largestUnit: "days" }).days; // 328

const today = Temporal.Now.plainDateISO();
Temporal.PlainDate.compare(today, start);  // -1, 0 or 1

start.add({ months: 1 }, { overflow: "reject" }); // RangeError

Objects are immutable, so add() returns a new date. Adding a month to January 31 clamps to the end of February by default, and with overflow: "reject" it throws instead, which is useful when you would rather know. until() and since() return a Duration, and largestUnit decides whether you get days or months and days.

Temporal has more types for other jobs: PlainTime, ZonedDateTime for a moment in a specific zone, and Instant for an exact timestamp. Picking the right one is most of the work.

Support is the catch. MDN lists Firefox 139 and Chrome 144, and Safari only in the 27.3 beta. Node.js 26 has it as well. For a site with many iPhone visitors I would use the official polyfill for now, or keep the date logic on the server.

Further reading: MDN: Temporal, MDN: Temporal.PlainDate

More Javascript tips node --inspect-brk app.js obj?.prop const filePath = String.raw`C:\Development\profile\about.html`; const observer = new ResizeObserver((entries) => { URL.parse("not a url"); // null All Javascript tips (40) All topics

Link