totalDuration
Signature
Section titled “Signature”totalDuration(value: string, unit: keyof DurationLikeObject | DateTimeUnit, options?: { relativeTo?: string; }): number | nullExpress an ISO 8601 duration as a single fractional total in one unit.
- Uses Temporal.Duration.from and .total — the whole duration is converted, so totalDuration(“P1DT2H30M”, “hours”) is 26.5, not the 2 that
getDurationUnitreads. - Which function to use:
totalDurationtakes an ISO 8601 duration string.totalUnitstakes a unit object of exact units, such as{ minutes: 15 }.durationFromUnitsturns a unit object into the duration string this function takes. To total a calendar amount written as a unit object, such as{ months: 1 }, pass the result ofdurationFromUnitshere withrelativeTo. - For a length counted from now, use
totalUnitsFromNow. The other forms aregetUnixIn(an epoch),getUtcIn(UTC text),formatHttpDateFromNow(an HTTP date) andttlFromNow(all of them from one clock read). - The result is fractional, not rounded: totalDuration(“P1DT2H30M”, “days”) is 1.1041666666666667. Round it yourself, or reach for
normalizeDurationinstead. It is a JavaScript number, so a total past 2^53 is the nearest double. - The calendar-unit rule of
relativeTois the same onenormalizeDuration(A3) carries;addDuration/subtractDuration(A2) have it worse still, since Temporal gives them norelativeTooption at all. - The requested-unit half of that rule bites even on day/time-only input: “P1DT2H30M” has no calendar component, yet totalDuration(“P1DT2H30M”, “weeks”) is still null — a week is a calendar quantity to Temporal regardless of what it is being measured from.
- A zoned
relativeTostring resolves its wall time with disambiguation “compatible” and offset “reject”, as Temporal does: an ambiguous wall time takes the earlier instant, a nonexistent one the later instant, and an offset that does not match the zone returns null. Pass an explicit offset to pick the other reading of an ambiguous wall time. - A
relativeTostring is ISO 8601 extended format over the whole string, and nothing else: a date, a date-time, or a zoned date-time (<date>T<time>, then nothing,Zor±HH:MM, then one time zone annotation). Basic format ("20240101"), a space or lower-casetseparator, a lower-casez, a UTC offset without a zone and a zoned date without a time are invalid, and so is a time zone annotation on a plain value. - A
relativeTostring takes a calendar in RFC 9557 form, asconvertDateToCalendarandconvertZonedToCalendarwrite it: ISO digits and one[u-ca=<id>]annotation, after the zone for a zoned string ("2024-02-24[u-ca=hebrew]","2024-02-24T00:00:00+02:00[Asia/Jerusalem][u-ca=hebrew]"). The critical flag and the id’s letter case are accepted. Any other annotation (an elective[foo=bar], a secondu-ca, an unknown critical one), a calendar before the zone, an unsupported calendar and a leap second in any spelling return null. Compatibility: before 1.16.0 the digits were read as the calendar’s own year, month and day. unitaccepts the plural or singular name ("hours"or"hour"), as Temporal does.- Returns null on a non-duration
value, an invalidunit, or an invalidrelativeTo.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value | string | ISO 8601 duration string |
unit | keyof DurationLikeObject | DateTimeUnit | DateTimeDurationUnit to total into (“years” | “months” | “weeks” | “days” | “hours” | “minutes” | “seconds” | “milliseconds” | “microseconds” | “nanoseconds”, or the singular name) |
Options
Section titled “Options”options
The anchor calendar units are measured from
| Option | Type | Default | Description |
|---|---|---|---|
relativeTo? | string (DurationRelativeTo) | None. Days are 24 hours, and a duration or unit that involves a year, month or week returns null. | The date or zoned date-time the duration is measured from. It is required whenever a year, month or week is involved, as the unit asked for or as a nonzero field of the duration. With a zoned value, days follow that zone’s clock changes, so a day spanning a spring-forward transition totals 23 hours. |
Returns
Section titled “Returns”the fractional total in unit, or null on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”import { totalDuration } from "@northguild/gmt/duration/calculate";
totalDuration("P1DT2H30M", "hours") // 26.5Source
Section titled “Source”Accessibility
© 2026 NorthGuild · Licences
Code examples: MIT-0. Packages: MIT. Third-party data and components: their own licences, credited where they appear. Everything else: © 2026 NorthGuild, all rights reserved.