Millwork codecs for Day.js. Each codec reads a wire value into a Dayjs
or a dayjs Duration, and writes it back.
pnpm add @millwork-ts/core @millwork-ts/dayjs dayjs
The codecs need the utc, timezone and duration plugins. This package calls dayjs.extend
for each of them when it loads, on the dayjs instance of your app. The package marks its entry
as a module with side effects, so a bundler keeps these calls. You do not have to extend
them yourself. It is safe to extend them again.
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 (dayjs:date). The name of each codec has the prefix.
If your app registers two date families, use the prefixed keys.
| Export | Key | Wire | Value |
|---|---|---|---|
date |
date |
"2026-12-01" |
Dayjs at local midnight |
datetime |
datetime |
RFC 3339, "2026-12-01T14:30:00-05:00" or "…Z" |
local Dayjs |
epoch |
epoch |
a number, or a string for large values | local Dayjs |
time |
time |
"14:30", "14:30:00.123" |
UTC Dayjs of today |
localDateTime |
local-datetime |
"2026-12-01T14:30:00" |
UTC Dayjs |
zonedDateTime |
zoned-datetime |
"2026-12-01T14:30:00-05:00[America/Toronto]", { at, zone } |
Dayjs in that zone (.tz()) |
yearMonth |
year-month |
"2026-12" |
Dayjs at local midnight of day 1 |
duration |
duration |
ISO 8601 "PT1H30M", or a number |
dayjs Duration |
dayjs has no interval type, so the package has no interval codec.
Options:
datetime: keepOffset: true keeps the wire offset in the value (utcOffset), and
serialize writes that offset. Without it, serialize writes Z. dayjs reads an offset of
16 minutes or less as hours, so such an offset is bad input.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. dayjs keeps milliseconds only.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.duration: unit is 'iso' (the default), 's' or 'ms'. A duration with years, months or
weeks cannot become a number, so serialize throws a RangeError. A day is 24 hours.dayjs arithmetic on a Duration (add, subtract) turns the result into years, months and
days of fixed length. So duration.hydrate('P10D').add(25, 'day') can serialize with a month in
it. Make a new duration from its units when the units are important.
time and local-datetime values are in UTC, so a DST gap in the local zone cannot move the
wall clock.
Bad wire input gives undefined and a report to onInvalid. A value of the wrong type, or an
invalid Dayjs, makes serialize throw a TypeError.
datetime and epoch compare the instant (valueOf()).zonedDateTime compares the instant and the zone.dayjsFamily.datetime({ strict: true }) (also epoch and zonedDateTime) gives a codec that
also compares the offset and the zone.date, time, local-datetime and year-month compare the wire text.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/dayjs'
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', 'dayjs:date'),
valueField('startsAt', 'dayjs:datetime', { keepOffset: true }),
valueField('length', 'dayjs:duration'),
],
})