@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.24 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isValidDateTime } from "../validate/isValidDateTime.js";
// src/plain/compare/isBeforeDateTime.ts
/**
* Return true when `value1` is strictly before `value2` for PlainDateTime values.
*
* - Uses Temporal.PlainDateTime.compare to compare date-times.
* - Returns false if either input is invalid.
*
* @param value1 ISO PlainDateTime string for the first value
* @param value2 ISO PlainDateTime string for the second value
* @returns boolean indicating whether value1 < value2
*
* @example isBeforeDateTime("2024-02-28T12:34:56", "2024-02-29T12:34:56") // true
* @example isBeforeDateTime("2024-02-29T12:34:56", "2024-02-29T12:34:56") // false
* @example isBeforeDateTime("2024-02-29T12:34:56", "2024-02-28T12:34:56") // false
* @example isBeforeDateTime("invalid", "2024-02-29T12:34:56") // false
* @example isBeforeDateTime("2024-02-29T12:34:56", "invalid") // false
*/
function isBeforeDateTime(value1, value2) {
if (!isValidDateTime(value1) || !isValidDateTime(value2)) return false;
try {
return Temporal.PlainDateTime.compare(Temporal.PlainDateTime.from(value1), Temporal.PlainDateTime.from(value2)) === -1;
} catch {
return false;
}
}
export { isBeforeDateTime };