@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.
37 lines (35 loc) • 1.43 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js";
import { isValidZonedDateTime } from "../validate/isValidZonedDateTime.js";
// src/zoned/compare/isBeforeZoned.ts
/**
* Return whether `value1` represents an instant strictly before `value2`.
*
* - Both inputs must be valid zoned ISO 8601 datetime strings.
* - Comparison is performed using Temporal.Instant (same instant semantics), so differing timeZone representations but the same instant will compare as equal.
* - Invalid inputs return `false`.
*
* @param value1 first zoned datetime string
* @param value2 second zoned datetime string
* @returns `true` if `value1` is before `value2`, otherwise `false`
*
* @example isBeforeZoned("2024-03-17T14:30:45+02:00[Africa/Johannesburg]", "2024-03-17T15:30:45+02:00[Africa/Johannesburg]") // true
* @example isBeforeZoned("invalid", "2024-03-17T14:30:45+02:00[Africa/Johannesburg]") // false
*/
function isBeforeZoned(value1, value2) {
if (!isValidZonedDateTime(value1) || !isValidZonedDateTime(value2)) return false;
let zonedDateTime1;
let zonedDateTime2;
try {
zonedDateTime1 = zonedDateTimeFrom(value1);
zonedDateTime2 = zonedDateTimeFrom(value2);
} catch {
return false;
}
try {
return Temporal.Instant.compare(zonedDateTime1.toInstant(), zonedDateTime2.toInstant()) === -1;
} catch {
return false;
}
}
export { isBeforeZoned };