Millwork codecs for date-fns. Each codec reads a wire value into a
native Date, a date-fns Duration or Interval, or a TZDate, and writes it back.
pnpm add @millwork-ts/core @millwork-ts/date-fns date-fns
For the zoned codec, also install @date-fns/tz and import from @millwork-ts/date-fns/tz:
pnpm add @date-fns/tz
The main entry does not import @date-fns/tz, so an app without it still works.
A Date is mutable, and Object.freeze does not stop its setters. So each codec gives a
FrozenDate (or a FrozenTZDate): a frozen subclass where each set… method throws a
TypeError. date-fns functions such as addDays give a plain, mutable Date back (a TZDate
in the same zone for a FrozenTZDate). To change a field, set a new value.
Each codec is a named export. The codecs record has each codec two times: with its kind as the
key (date) and with the adapter prefix (date-fns:date). The name of each codec has the
prefix. If your app registers two date families, use the prefixed keys.
From @millwork-ts/date-fns:
| Export | Key | Wire | Value |
|---|---|---|---|
date |
date |
"2026-12-01" |
FrozenDate at local midnight |
datetime |
datetime |
RFC 3339, "2026-12-01T14:30:00-05:00" or "…Z" |
FrozenDate |
epoch |
epoch |
a number, or a string for large values | FrozenDate |
duration |
duration |
ISO 8601 "PT1H30M", or a number |
frozen date-fns Duration |
interval |
interval |
"a/b", "a/PT1H", "P1D/b", { start, end } |
frozen date-fns Interval of dates |
From @millwork-ts/date-fns/tz, with all of the above:
| Export | Key | Wire | Value |
|---|---|---|---|
zonedDateTime |
zoned-datetime |
"2026-12-01T14:30:00-05:00[America/Toronto]", { at, zone } |
FrozenTZDate |
date-fns has no time, local date-time or year-month type, so the package has no codec for them.
Options:
date: the value is local midnight. It is never made with new Date("2026-12-01"), which gives
UTC midnight and the day before in western zones.datetime: a Date has no offset, so serialize always writes Z. The keepOffset option
has no effect. Digits below the millisecond are dropped.epoch: unit is 's', 'ms' (the default), 'us' or 'ns'. serialize writes a number
when it is a safe integer. It writes 'ns' as a string.duration: unit is 'iso' (the default), 's' or 'ms'. Parts of a second go in
seconds. A duration with years, months or weeks cannot become a number, so serialize
throws a RangeError. A day is 24 hours.interval: wire: 'object' writes { start, end }. A duration is added in UTC. An open
interval (..) is bad input. The interval is half-open: [start, end).zonedDateTime: wire: 'object' writes { at, zone }. The default writes RFC 9557. Both are
read. The wire offset must be the offset of the zone at that instant, or the value is bad.Bad wire input gives undefined and a report to onInvalid. A value of the wrong type, or an
Invalid Date, makes serialize throw a TypeError. serialize accepts any valid Date, not
only a FrozenDate.
datetime, epoch and interval compare the instant (getTime()).zonedDateTime compares the instant and the zone.date compares the local calendar day.duration compares years, months and weeks one by one, and the other units by length.
PT1H equals PT60M. P1M does not equal P30D.Register the codecs before the store calls transformations(), because transformations()
reads the registry when it runs. See the
@millwork-ts/warp-drive README.
import { codecs } from '@millwork-ts/date-fns/tz'
import { registerCodecs, valueField } from '@millwork-ts/warp-drive'
import { withDefaults } from '@warp-drive/core/reactive'
registerCodecs(codecs)
export const EventSchema = withDefaults({
type: 'event',
fields: [
valueField('startsOn', 'date-fns:date'),
valueField('startsAt', 'date-fns:datetime'),
valueField('local', 'date-fns:zoned-datetime'),
],
})