diffDateTime
Signature
Section titled “Signature”diffDateTime(dateTime1: string, dateTime2: string, units: any, options?: RoundingOptions<Temporal.DateTimeUnit>): number | Record<DateTimeDurationUnit, number>import { diffDateTime } from "@northguild/gmt/plain/calculate";Return the difference between two PlainDateTime values in the requested unit.
- Returns
nullfor invalid inputs (negative diffs are valid). - Uses Temporal.PlainDateTime.until and extracts the requested unit.
smallestUnit,roundingIncrement, androundingModecontrol 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
unitsis an array,smallestUnitmust not be coarser than the largest unit in the array (e.g.["day", "hour"]withsmallestUnit: "week") — this combination is rejected by Temporal and returns null, same as other invalid input.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
dateTime1 |
string |
ISO PlainDateTime string for the start |
dateTime2 |
string |
ISO PlainDateTime string for the end |
units |
any |
DateTimeDurationUnit | DateTimeDurationUnit[] to measure the difference |
options |
RoundingOptions<Temporal.DateTimeUnit> |
optional: smallestUnit, roundingIncrement, roundingMode (Temporal.DifferenceOptions rounding controls) |
Returns
Section titled “Returns”numeric difference in the requested unit, or null on invalid input
Related types
Section titled “Related types”Examples
Section titled “Examples”diffDateTime("2024-03-10T12:00:00", "2024-03-15T12:00:00", "day") // 5diffDateTime("invalid", "2024-03-15T12:00:00", "day") // null