Development How-to

Your agent's renewal date lands in March: pin month-end and DST bugs, then move the module to Temporal

Agent-written TypeScript still does date maths with setMonth and milliseconds. Five failing tests show where it breaks, and a Temporal rewrite fixes it in about 20 minutes.

A tall paper sheet with a teal strip that stops cleanly at the end of a shorter sheet in front of it

A subscription renews monthly. The customer signed up on 31 January, and the agent-written helper says the next renewal is 3 March. February simply didn’t happen for them. Nobody notices until billing support does.

The helper is three lines of the kind agents produce by default: copy the date, call setMonth, return it. It reads fine, it compiles, and the first test the agent writes (15 March plus one month) passes. The bug lives on the days nobody picks as an example.

Here’s how to pin it with failing tests first, let the agent rewrite the module on Temporal, and keep the old code around as a witness. The method works with any coding agent that can read files and run a shell, because the instruction, the tests and the check are all in the repo. The example is TypeScript with Vitest.

The code an agent writes first

This is the module as a first draft usually looks:

export function nextRenewal(from: Date, months: number): Date {
  const d = new Date(from);
  d.setMonth(d.getMonth() + months);
  return d;
}

export function daysUntil(from: Date, to: Date): number {
  return Math.floor((to.getTime() - from.getTime()) / 86_400_000);
}

Two defects, both well known and both easy to generate. setMonth doesn’t clamp: it sets the month and lets the day overflow, so 31 January plus one month becomes 31 February, which the engine normalises to 3 March. And daysUntil assumes a day is 86,400,000 milliseconds, which stops being true twice a year in any zone with daylight saving.

Write the cases as data, not as prose

Put the expected answers in one file that both the old and new implementation can be tested against. Dates and expected results only, no calls:

// src/cases.ts
export const renewals: [from: string, months: number, want: string][] = [
  ["2026-01-31", 1, "2026-02-28"],
  ["2028-01-31", 1, "2028-02-29"],
  ["2026-08-31", 1, "2026-09-30"],
  ["2026-12-15", 2, "2027-02-15"],
];

// Calendar days between two local dates in America/New_York.
// The clocks go forward on 8 March 2026.
export const spans: [from: string, to: string, want: number][] = [
  ["2026-03-07", "2026-03-09", 2],
  ["2026-03-01", "2026-03-31", 30],
];

The rule behind the first group is one sentence a human can check: when the target month is shorter, the renewal falls on its last day. The second group is plain arithmetic on the calendar. Writing the expectations by hand matters. If the agent derives them by running its own function, the bug gets baked into the test.

Now the witness: a test file that runs those cases against the legacy functions, building dates the way real callers do, as local midnight.

// src/renewal.legacy.test.ts
import { it, expect } from "vitest";
import { nextRenewal, daysUntil } from "./renewal.legacy";
import { renewals, spans } from "./cases";

const local = (s: string) => {
  const [y, m, d] = s.split("-").map(Number);
  return new Date(y, m - 1, d);
};
const ymd = (x: Date) =>
  `${x.getFullYear()}-${String(x.getMonth() + 1).padStart(2, "0")}-${String(x.getDate()).padStart(2, "0")}`;

it.each(renewals)("legacy: %s + %i month(s) = %s", (from, months, want) => {
  expect(ymd(nextRenewal(local(from), months))).toBe(want);
});

it.each(spans)("legacy: %s to %s is %i days", (from, to, want) => {
  expect(daysUntil(local(from), local(to))).toBe(want);
});

Run it with a zone that observes daylight saving:

TZ=America/New_York npx vitest run
 × legacy: 2026-01-31 + 1 month(s) = 2026-02-28
 × legacy: 2028-01-31 + 1 month(s) = 2028-02-29
 × legacy: 2026-08-31 + 1 month(s) = 2026-09-30
 × legacy: 2026-03-07 to 2026-03-09 is 2 days
 × legacy: 2026-03-01 to 2026-03-31 is 30 days

AssertionError: expected '2026-03-03' to be '2026-02-28'
AssertionError: expected '2028-03-02' to be '2028-02-29'
AssertionError: expected '2026-10-01' to be '2026-09-30'
AssertionError: expected 1 to be 2
AssertionError: expected 29 to be 30

Five failures, and none of them is a typo. The 31 August case is the sneaky one: it lands on 1 October, so the customer is billed a day late and the following month’s renewal drifts with it. The last failure is worth a second look. A 30-day span reports 29, because the month contains a 23-hour day and Math.floor rounds the 29.96 down.

The same suite is green in UTC

Run the identical file with TZ=UTC and the two span tests pass. Only the three month-end tests fail, since they never depended on the zone.

That’s the reason this class of bug survives review. CI runners are almost always UTC. The developer’s laptop isn’t, and neither is the customer. A test suite that only ever runs in UTC can’t see the DST failures, so run at least one job with a zone that has daylight saving (America/New_York or Europe/London), and say so in the repo’s contributing notes so the agent copies it.

Give the agent a narrow brief

With the failing tests committed, hand over the rewrite. A prompt that works with any agent:

src/renewal.legacy.ts has two date helpers that fail the tests in
src/renewal.legacy.test.ts when run with TZ=America/New_York. Do not edit
src/cases.ts or either test file.

Write src/renewal.ts with the same two functions on Temporal.PlainDate
(month-end dates clamp to the last day of the shorter month; daysUntil counts
calendar days). Write src/renewal.test.ts that runs the cases in src/cases.ts
against the new functions. Run `TZ=America/New_York npx vitest run` and show me
the output. Don't touch any other file.

The constraints do the work. “Don’t edit the cases” stops the agent from making a red test green by changing the expectation. “Same two functions” keeps the diff reviewable. Naming the zone on the command line means the agent reproduces the failure before it fixes anything.

What the Temporal version looks like

Node 26.0.0 shipped on 5 May 2026 with Temporal enabled by default, according to its release notes, and 26.x is in long-term support now. On older Node, the @js-temporal/polyfill package (ISC licence, version 0.5.1 when this was written) gives the same API through an import. The module below uses the import, so it runs on both. On Node 26 you can delete the first line and use the global.

import { Temporal } from "@js-temporal/polyfill";

/** Renewal date `months` after `from`; Jan 31 + 1 month is the last day of February. */
export function nextRenewal(from: Temporal.PlainDate, months: number): Temporal.PlainDate {
  return from.add({ months }); // overflow: "constrain" is the default
}

export function daysUntil(from: Temporal.PlainDate, to: Temporal.PlainDate): number {
  return from.until(to, { largestUnit: "days" }).days;
}

Temporal.PlainDate is a calendar date with no time and no zone, which is exactly what a renewal date is. There’s no midnight to drift across and no 23-hour day to trip on. add({ months: 1 }) clamps by default: MDN’s page for PlainDate.add documents overflow: "constrain" as the default and shows 31 January plus one month giving 28 February.

The new test file is short, because the cases live elsewhere:

import { it, expect } from "vitest";
import { Temporal } from "@js-temporal/polyfill";
import { daysUntil, nextRenewal } from "./renewal";
import { renewals, spans } from "./cases";

const d = (s: string) => Temporal.PlainDate.from(s);

it.each(renewals)("%s + %i month(s) = %s", (from, months, want) => {
  expect(nextRenewal(d(from), months).toString()).toBe(want);
});

it.each(spans)("%s to %s is %i days", (from, to, want) => {
  expect(daysUntil(d(from), d(to))).toBe(want);
});
 Test Files  1 passed (1)
      Tests  6 passed (6)

Six of six pass in America/New_York. They’d pass in any zone, since nothing in the new code reads the system clock’s offset.

Check what the agent did

Three things to look at in the diff before merging.

  • Whether the agent changed cases.ts or loosened an assertion. It shouldn’t have, and git diff --stat shows it in one line.
  • Whether callers were updated. Every call site that passed a Date now needs a PlainDate. The compiler finds them, which is a good reason to keep tsc --noEmit in the loop.
  • Whether the agent reached for .toZonedDateTimeISO() or .toPlainDate() at the edges without thinking about which zone. The conversion from an instant to a calendar date needs the customer’s zone, not the server’s. That’s a product decision, and the agent can’t make it for you.

When to ask for reject instead

The default constrain is right for renewals, where the business rule is “last day of the month”. It’s wrong when a day that doesn’t exist is a data error, such as a user-entered birth date of 31 April. For that, pass { overflow: "reject" } and add throws a RangeError instead of silently picking a neighbour. Decide per field, and put the decision in a test name so it survives the next refactor.

Where this doesn’t apply

  • Timestamps for events (logs, audit trails, “created at”) are instants, and Date or Temporal.Instant is fine. The trouble starts when an instant is used as a calendar date.
  • A browser app that supports older engines can’t rely on the global yet. Use the polyfill, check bundle size, and keep the same tests.
  • A codebase that already uses a date library with clamping semantics doesn’t need a migration. It needs the five tests, to confirm the library does what the team thinks.

Try it now

Search the repo for setMonth(, setDate( and 86400000 (or 86_400_000). Pick one hit, write its month-end and DST cases by hand in a cases.ts, run them with TZ=America/New_York, and only then give the agent the rewrite.