Sub topic 10
Adding, Subtracting, and Replacing Fields
This chapter assumes you can create Temporal values with from() and know that Temporal objects are immutable. The examples import the polyfill so they run in any Node.js version. If your runtime has native Temporal, you can drop the import.
Three methods, one rule
Every Temporal date and time type has three methods for producing a changed value:
| Method | What it does | Example argument |
|---|---|---|
| `add()` | Moves forward by a duration | `{ days: 10 }` |
| `subtract()` | Moves backward by a duration | `{ months: 2 }` |
| `with()` | Replaces named fields, leaving the rest | `{ day: 1 }` |
None of these methods changes the original. Each one returns a new object, so always use the return value.
import { Temporal } from '@js-temporal/polyfill';
const due = Temporal.PlainDate.from('2024-03-15');
const later = due.add({ weeks: 2 });
const earlier = due.subtract({ days: 10 });
const firstOfMonth = due.with({ day: 1 });
const endOfMonth = due.with({ day: due.daysInMonth });
console.log(later.toString()); // '2024-03-29'
console.log(earlier.toString()); // '2024-03-05'
console.log(firstOfMonth.toString()); // '2024-03-01'
console.log(endOfMonth.toString()); // '2024-03-31'
console.log(due.toString()); // '2024-03-15' (unchanged)The argument to add() and subtract() can be a property bag, a Temporal.Duration, or an ISO 8601 duration string such as 'P1M'. subtract({ days: 3 }) gives the same result as add({ days: -3 }).
Calendar units are applied largest first
When one call mixes units, Temporal adds years first, then months, then weeks and days. If the month step lands on a day that doesn't exist, the day is clamped to the end of the month before the days are added. The next chapter covers that clamping in detail.
Because of this order, one combined call can give a different result than two separate calls:
import { Temporal } from '@js-temporal/polyfill';
const jan30 = Temporal.PlainDate.from('2024-01-30');
const combined = jan30.add({ months: 1, days: 1 });
const daysFirst = jan30.add({ days: 1 }).add({ months: 1 });
console.log(combined.toString()); // '2024-03-01' (Feb 29, then +1 day)
console.log(daysFirst.toString()); // '2024-02-29' (Jan 31, then +1 month)Times wrap around
A PlainTime has no date, so arithmetic on it wraps at midnight and discards the extra days. If you need the date to roll over too, use PlainDateTime.
import { Temporal } from '@js-temporal/polyfill';
const late = Temporal.PlainTime.from('23:30');
console.log(late.add({ hours: 1 }).toString()); // '00:30:00'
const shift = Temporal.PlainDateTime.from('2024-03-15T23:30');
console.log(shift.add({ hours: 1 }).toString()); // '2024-03-16T00:30:00'
console.log(shift.with({ hour: 9, minute: 0 }).toString()); // '2024-03-15T09:00:00'Common errors
- Ignoring the return value.
date.add({ days: 1 })on its own line does nothing useful. - Using singular unit names in
add(). Duration bags take plural keys (days,months). Field bags forwith()take singular keys (day,month). - Calling
with()with no known fields.date.with({})throws aTypeError. - Adding time units to a
PlainDate. Hours smaller than a full day are ignored, soadd({ hours: 23 })leaves the date unchanged.
Exercise
Given const d = Temporal.PlainDate.from('2024-08-31'), what do d.add({ months: 1, days: 1 }) and d.with({ month: 2 }) return, and why?
Show answer
d.add({ months: 1, days: 1 }) returns '2024-10-01'. Adding the month first gives September 31, which doesn't exist and is clamped to September 30. Adding one day then gives October 1. d.with({ month: 2 }) returns '2024-02-29' because February 31 is clamped to the last day of February in the leap year 2024.
Exercise
Write one expression that returns the last day of the month after d, for any PlainDate d.
Show answer
d.with({ day: 1 }).add({ months: 1 }).with({ day: 31 }). Starting from the first of the month avoids clamping problems when the month is added, and with({ day: 31 }) clamps to the real last day. Another valid answer is const next = d.with({ day: 1 }).add({ months: 1 }); next.with({ day: next.daysInMonth }).