Skip to content

Durations

A ZDuration is an amount of time in calendar and clock units — years through nanoseconds. It stands alone: durations are created from strings or fields, combined, normalized, and formatted without needing a date to exist.

ts
import {ZDuration} from '@sajajs/zdate'

ZDuration.from('2 days, 3 hours')
ZDuration.from({days: 2, hours: 3})
zdate().diff(deadline)               // verbs return durations

Fields

years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds (bigint), nanoseconds (bigint) — each available as a getter, all together via toFields().

Operations

MethodDescription
by(other)Combine with another duration or fields
negate()Flip the sign of every field
abs()Absolute value of every field
normalize()Carry overflow between units (90 minutes -> 1 hour 30 minutes)
collapse(unit?)Collapse to a single unit
equals(other)Field equality
isZero() / isNegative()Predicates

Output

MethodExample
toISO()'P2DT3H' — ISO 8601 duration
toString()'2 days, 3 hours'
toCompact()'2d 3h'
toRelative(options?)'in 2 days' / '2 days ago' — localized
toJSON()Structured fields

Locale

ZDuration.from() accepts a locale for parsing localized unit words, and toString()/toRelative() render in the duration's locale.