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, throwing an error if calendar and clock fields end up with opposite signs. Accepts the same inputs as plus(), and is also aliased as subtract() and remove().
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.
hours(2).minus('30m').toString(); // 'PT1H30M'
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.
Months and years need a starting date
Standalone calendar spans like months(1) have no fixed day count, so converting them to days or milliseconds throws a RangeError. Measure between two dates with until() when you need exact totals across months or years.
balance()
dur.balance(largestUnit?): Duration
Carries overflow from smaller units into larger ones. By default, standalone durations balance up to hours and 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.