diffDateTimeAsDuration
Signature
Section titled “Signature”diffDateTimeAsDuration(dateTime1: string, dateTime2: string, unit: keyof DurationLike | DateTimeUnit, options?: (RoundingOptions<DateTimeUnit> & DurationStringOptions)): stringReturn the difference between two PlainDateTime values as an ISO 8601 duration string, bridging to the duration namespace (see parseDuration, 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 | keyof DurationLike | DateTimeUnit | DateTimeDurationUnit to use as the duration’s largestUnit |
Options
Section titled “Options”options
optional: how the difference is rounded and how the string is rendered; a non-object value (such as null) is invalid
| Option | Type | Default | Description |
|---|---|---|---|
smallestUnit? | DateTimeUnit | PluralUnit<DateTimeUnit> | None. The difference is not rounded: Temporal uses the smallest unit the values have, "day" for dates and "nanosecond" otherwise. | The smallest unit kept in the difference; anything smaller is rounded into it. It may not be larger than the largest unit asked for. |
roundingIncrement? | number | 1, Temporal’s default. | The step the smallest unit is rounded to, so 15 with "minute" rounds to a quarter of an hour. For a time unit it must divide the next larger unit evenly and be smaller than it (15 or 30 minutes, not 7 or 60); a date unit takes any whole number from 1 up. A non-integer is truncated first, as Temporal does, and any other value is invalid input. |
roundingMode? | "ceil" | "floor" | "expand" | "trunc" | "halfCeil" | "halfFloor" | "halfExpand" | "halfTrunc" | "halfEven" | "trunc", Temporal’s default for a difference. | Which way a value between two steps goes. "trunc" rounds toward zero and "halfExpand" to the nearest step, a half going away from zero; the other modes are those of Temporal’s roundingMode option. |
toStringSmallestUnit? | SmallestUnit<"minute" | "second" | "millisecond" | "microsecond" | "nanosecond"> | None. The precision comes from fractionalSecondDigits. | The smallest unit written in the string: "second", "millisecond", "microsecond" or "nanosecond". It sets how many fractional-second digits appear and takes precedence over fractionalSecondDigits. "minute" is refused, as Temporal.Duration refuses it. |
fractionalSecondDigits? | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | "auto" | 8 | 9 | "auto", Temporal’s default. | The number of fractional-second digits written: 0 to 9 writes exactly that many, and "auto" writes as many as the duration needs, with no trailing zeros. It has no effect when toStringSmallestUnit is given. |
toStringRoundingMode? | RoundingMode | "trunc", Temporal’s default. | How the duration is rounded when the precision asked for drops digits. "trunc" cuts them off; "halfExpand" rounds to the nearest value, a half going away from zero. |
Returns
Section titled “Returns”ISO 8601 duration string, or "" on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”import { diffDateTimeAsDuration } from "@northguild/gmt/plain/calculate";
diffDateTimeAsDuration("2024-03-10T00:00:00", "2024-03-11T02:00:00", "days") // "P1DT2H"