@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.
32 lines (30 loc) • 1.28 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { plainDateTimeOf } from "../../internal/inputChecks.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) {
const value1Parsed = plainDateTimeOf(value1);
const value2Parsed = plainDateTimeOf(value2);
if (value1Parsed === null || value2Parsed === null) return false;
try {
return Temporal.PlainDateTime.compare(value1Parsed, value2Parsed) === -1;
} catch {
return false;
}
}
export { isBeforeDateTime };