diffDateTimeAsDuration
Signature
Section titled “Signature”diffDateTimeAsDuration(dateTime1: string, dateTime2: string, unit: DateTimeDurationUnit, options?: any): stringimport { diffDateTimeAsDuration } from "@northguild/gmt/plain/calculate";Return the difference between two PlainDateTime values as an ISO 8601 duration string,
- bridging to the
durationnamespace (seeparseDuration,normalizeDuration). - Returns
""for invalid inputs (negative diffs are valid and render with a leading-). - Uses Temporal.PlainDateTime.until with
largestUnitset tounit, then.toString(). - Unlike
diffDateTime,unitis a single unit (not an array) — an ISO duration string already expresses a full multi-unit breakdown vialargestUnitalone, so there’s no array-of-units overload here.smallestUnit,roundingIncrement, androundingModecontrol optional rounding of the underlying difference before it’s rendered, per Temporal’s DifferenceOptions — same asdiffDateTime.toStringSmallestUnit,fractionalSecondDigits, andtoStringRoundingModecontrol the precision of the rendered string itself, per Temporal’s ToStringPrecisionOptions (mirroringparseDuration’s options) — kept separate from the.until()rounding options above because both option sets have collidingsmallestUnit/roundingModekeys with different Temporal types.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
dateTime1 |
string |
ISO PlainDateTime string for the start |
dateTime2 |
string |
ISO PlainDateTime string for the end |
unit |
DateTimeDurationUnit |
DateTimeDurationUnit to use as the duration’s largestUnit |
options |
any |
optional: smallestUnit, roundingIncrement, roundingMode (.until() rounding); toStringSmallestUnit, fractionalSecondDigits, toStringRoundingMode (.toString() precision) |
Returns
Section titled “Returns”ISO 8601 duration string, or “” on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”diffDateTimeAsDuration("2024-03-10T00:00:00", "2024-03-11T02:00:00", "days") // "P1DT2H"diffDateTimeAsDuration("2024-03-11T02:00:00", "2024-03-10T00:00:00", "days") // "-P1DT2H"diffDateTimeAsDuration("2024-01-01T00:00:00", "2024-01-01T00:00:00", "days") // "PT0S"diffDateTimeAsDuration("invalid", "2024-03-15T12:00:00", "days") // ""diffDateTimeAsDuration("2024-02-29T00:00:00", "2024-02-29T01:30:00", "hours", { smallestUnit: "hours", roundingMode: "halfExpand" }) // "PT2H"