@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.
30 lines (28 loc) • 1.22 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { zonedDateTimeOf } from "../../internal/zonedWallClock.js";
// src/zoned/compare/isAfterZoned.ts
/**
* Return whether `value1` represents an instant strictly after `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 after `value2`, otherwise `false`
*
* @example isAfterZoned("2024-02-29T12:34:56.789+00:00[UTC]", "2024-02-29T12:34:56.788+00:00[UTC]") // true
* @example isAfterZoned("invalid", "2024-02-29T12:34:56.789+00:00[UTC]") // false
*/
function isAfterZoned(value1, value2) {
const zonedDateTime1 = zonedDateTimeOf(value1);
const zonedDateTime2 = zonedDateTimeOf(value2);
if (zonedDateTime1 === null || zonedDateTime2 === null) return false;
try {
return Temporal.Instant.compare(zonedDateTime1.toInstant(), zonedDateTime2.toInstant()) === 1;
} catch {
return false;
}
}
export { isAfterZoned };