Skip to content

getDurationUnit

  • The core functions, which every industry uses.
getDurationUnit(value: string, unit: keyof DurationLikeObject | DateTimeUnit): number | null

Read a single component out of an ISO 8601 duration string.

  • Uses Temporal.Duration.from and reads the named field directly — this is the value as stored, not a converted total: getDurationUnit(“PT90M”, “hours”) is 0, because “PT90M” holds 90 in its minutes field and nothing in its hours field. Use totalDuration for a converted total.
  • Never needs relativeTo: reading a field is not a unit conversion, so calendar units (years/months/weeks) work here even though they require an anchor in totalDuration.
  • Temporal balances fractional units at parse time, so getDurationUnit(“PT1.5H”, “minutes”) is 30 — the input’s own precision, not the caller’s spelling of it, decides the fields.
  • A negative duration stores every field as negative, so this returns a negative number for each nonzero component of “-P1DT2H”, not just the leading one.
  • unit accepts the plural or singular name ("hours" or "hour"), as Temporal does.
  • Returns null when value is not a valid duration string or unit is not a valid DateTimeDurationUnit.
ParameterTypeDescription
valuestringISO 8601 duration string
unitkeyof DurationLikeObject | DateTimeUnitDateTimeDurationUnit to read (“years” | “months” | “weeks” | “days” | “hours” | “minutes” | “seconds” | “milliseconds” | “microseconds” | “nanoseconds”, or the singular name)

the component’s value, or null on invalid input

import { getDurationUnit } from "@northguild/gmt/duration/calculate";
getDurationUnit("P1DT2H30M", "hours") // 2
output 

duration/calculate/getDurationUnit.ts

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.