Parse phrases and ISO 8601 strings with duration(), call unit helpers like hours(), or measure the span between two dates or times. Instances are immutable, so every method returns a new value.
Creating a duration from 90 minutes keeps minutes at 90 rather than converting to 1 hour and 30 minutes. Calendar days also stay separate from hours across daylight saving changes until you call balance().
Properties
Properties reflect the unit counts stored on the duration.
Property
Type
Description
text
string
Locale-formatted duration in long style, such as '1 hour, 30 minutes'.
years, months, weeks, days
number
Signed calendar unit counts stored on this duration.
hours, minutes, seconds
number
Signed clock unit counts.
milliseconds, microseconds, nanoseconds
number
Signed sub-second unit counts.
sign
number
Overall direction of the duration: 1, 0, or -1.
anchor
TemporalValue
Starting point recorded by until() or since(), or undefined on standalone durations.
Arithmetic and sign
Combine durations, scale them by a factor, or flip their sign. All non-zero fields in a duration must share the same sign.
Subtracts another duration, borrowing across years and months or weeks and days when needed. Incompatible opposite-sign pairs like days minus hours throw a RangeError, and subtract() works as an alias.
Parameters
otherDurationInputrequired
Count paired with unit, Duration instance, duration string, fields object, or milliseconds.
unitUnit
Unit name when other is a number.
Returns
Duration
A new Duration with other subtracted.
import { months, weeks, years } from 'native-dates';hours(2).minus('30m').toString(); // 'PT1H30M'years(1).minus(months(1)).toString(); // 'P11M'weeks(1).minus(days(1)).toString(); // 'P6D'
times()
dur.times(factor): Duration
Multiplies every field by a number, spilling fractional results into smaller units.
Parameters
factornumberrequired
Multiplier to scale the duration by, including fractional or negative numbers.
Roll smaller units up into larger ones, round to a target precision, or express the entire span as a single number.
Converting months and years to days
Pure month and year durations balance, total, and compare among themselves using 12 months per year. Converting them to weeks, days, or clock units requires a starting date from until() or since(), since calendar months vary in day count.
balance()
dur.balance(largestUnit?): Duration
Carries overflow from smaller units into larger ones. By default, standalone durations roll clock units up to hours and months into years, while measured spans balance up to days unless you pass a larger target unit.
Parameters
largestUnitUnit
Largest unit to roll values up into, such as 'hour', 'day', or 'week'.
Formats the duration in the active locale using Intl.DurationFormat. Clock overflow balances up to hours automatically, and sub-second fields stay hidden whenever larger units are present.
Reads the locale carried on this duration, or returns a copy formatted in another locale across text previews, presets, and arithmetic chains. Passing an empty tag clears the carried locale, and lang() works as an alias.
Parameters
tagLocale | null | undefined
BCP 47 locale tag to carry forward, or null to clear an existing tag.
Returns
Duration | Locale | undefined
The active tag when called with no arguments, or a new Duration carrying tag.
import { days } from 'native-dates';const wait = days(2).locale('ar');wait.locale(); // 'ar'wait.text; // 'يومان'