@burglekitt/gmt
Version:
Temporal-based date and time utilities with timezone support and polyfill integration
28 lines (27 loc) • 1.8 kB
TypeScript
import { Temporal } from "@js-temporal/polyfill";
import type { DateTimeDurationUnit, RoundingOptions } from "../../types/index.js";
/**
* Return the difference between two UTC datetimes measured in the given date-time unit.
*
* - Uses Temporal.Instant.until() to calculate the difference.
* - Supports single unit or array of units.
* - Returns null for invalid input.
*
* `smallestUnit`, `roundingIncrement`, and `roundingMode` control optional rounding of the result,
* per Temporal's DifferenceOptions — e.g. `{ smallestUnit: "hour", roundingMode: "halfExpand" }`
* rounds the difference to the nearest hour before extracting the requested unit.
* - When `units` is an array, `smallestUnit` must not be coarser than the largest unit in the
* array (e.g. `["day", "hour"]` with `smallestUnit: "week"`) — this combination is rejected by
* Temporal and returns null, same as other invalid input.
*
* @param value1 UTC ISO datetime string (start)
* @param value2 UTC ISO datetime string (end)
* @param units DateTimeDurationUnit | DateTimeDurationUnit[] to measure the difference
* @param options optional: smallestUnit, roundingIncrement, roundingMode (Temporal.DifferenceOptions rounding controls)
* @returns numeric difference in the requested unit, or null on invalid input
*
* @example diffUtc("2024-03-10T12:00:00Z", "2024-03-11T12:00:00Z", "hour") // 24
* @example diffUtc("2024-03-10T12:00:00Z", "2025-04-10T12:00:00Z", ["year", "month"]) // { year: 1, month: 1 }
* @example diffUtc("invalid", "2024-03-11T12:00:00Z", "hour") // null
*/
export declare function diffUtc(value1: string, value2: string, units: DateTimeDurationUnit | DateTimeDurationUnit[], options?: RoundingOptions<Temporal.DateTimeUnit>): number | Record<DateTimeDurationUnit, number> | null;