@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
52 lines (50 loc) • 3 kB
JavaScript
import { plainDateTimeOf } from "../../internal/inputChecks.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { durationUntilString } from "../../internal/durationUntilString.js";
import { isDateTimeDurationUnitName } from "../../internal/unitNames.js";
import { resolveDurationUnit } from "../../internal/resolveDurationUnit.js";
// src/plain/calculate/diffDateTimeAsDuration.ts
/**
* Return 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 `largestUnit` set to `unit`, then `.toString()`.
* - Unlike `diffDateTime`, `unit` is a single unit (not an array) — an ISO duration string
* already expresses a full multi-unit breakdown via `largestUnit` alone, so there's no
* array-of-units overload here.
*
* `smallestUnit`, `roundingIncrement`, and `roundingMode` control optional rounding of the
* underlying difference before it's rendered, per Temporal's DifferenceOptions — same as
* `diffDateTime`. `toStringSmallestUnit`, `fractionalSecondDigits`, and `toStringRoundingMode`
* control the precision of the rendered string itself, per Temporal's ToStringPrecisionOptions
* (mirroring `parseDuration`'s options) — kept separate from the `.until()` rounding options
* above because both option sets have colliding `smallestUnit`/`roundingMode` keys with
* different Temporal types.
*
* @param dateTime1 ISO PlainDateTime string for the start
* @param dateTime2 ISO PlainDateTime string for the end
* @param unit DateTimeDurationUnit to use as the duration's largestUnit
* @param options optional: how the difference is rounded and how the string is rendered; a non-object value (such as `null`) is invalid
* @returns ISO 8601 duration string, or "" on invalid input
*
* @example diffDateTimeAsDuration("2024-03-10T00:00:00", "2024-03-11T02:00:00", "days") // "P1DT2H"
* @example diffDateTimeAsDuration("2024-03-11T02:00:00", "2024-03-10T00:00:00", "days") // "-P1DT2H"
* @example diffDateTimeAsDuration("2024-01-01T00:00:00", "2024-01-01T00:00:00", "days") // "PT0S"
* @example diffDateTimeAsDuration("invalid", "2024-03-15T12:00:00", "days") // ""
* @example diffDateTimeAsDuration("2024-02-29T00:00:00", "2024-02-29T01:30:00", "hours", { smallestUnit: "hours", roundingMode: "halfExpand" }) // "PT2H"
*/
function diffDateTimeAsDuration(dateTime1, dateTime2, unit, options) {
if (!isOptionsArgument(options)) return "";
const dt1 = plainDateTimeOf(dateTime1);
const dt2 = plainDateTimeOf(dateTime2);
const resolvedUnit = resolveDurationUnit(unit);
const validUnit = isDateTimeDurationUnitName(resolvedUnit);
if (dt1 === null || dt2 === null || !validUnit) return "";
try {
return durationUntilString(dt1, dt2, resolvedUnit, options);
} catch {
return "";
}
}
export { diffDateTimeAsDuration };