diffDateAsDuration
Signature
Section titled “Signature”diffDateAsDuration(date1: string, date2: string, unit: DateDurationUnit, options?: any): stringimport { diffDateAsDuration } from "@northguild/gmt/plain/calculate";Return the difference between two PlainDate 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.PlainDate.until with
largestUnitset tounit, then.toString(). - Accepts GMT calendar-annotated PlainDate strings — E5 (issue #78). Same shared-vs-mismatched calendar rule as
diffDate(see its JSDoc): measured in the shared calendar whendate1anddate2carry the same tag, Gregorian otherwise. - Unlike
diffDate,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 asdiffDate.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 |
|---|---|---|
date1 |
string |
ISO PlainDate string for the start, optionally calendar-annotated |
date2 |
string |
ISO PlainDate string for the end, optionally calendar-annotated |
unit |
DateDurationUnit |
DateDurationUnit 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”diffDateAsDuration("2024-03-10", "2024-04-05", "days") // "P26D"diffDateAsDuration("2024-01-01", "2023-01-01", "days") // "-P365D"diffDateAsDuration("2024-01-01", "2024-01-01", "days") // "PT0S"diffDateAsDuration("invalid", "2024-03-15", "days") // ""diffDateAsDuration("2024-01-01", "2024-01-16", "weeks", { smallestUnit: "weeks", roundingMode: "halfExpand" }) // "P2W"diffDateAsDuration("5784-06-15[u-ca=hebrew]", "5784-07-15[u-ca=hebrew]", "months") // "P1M" (measured in Hebrew, Adar I -> Adar)