@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.
29 lines (27 loc) • 1.38 kB
JavaScript
import { plainDateTimeOf } from "../../internal/inputChecks.js";
// src/plain/compare/areDateTimesEqual.ts
/**
* Return true if two PlainDateTime ISO strings represent the same instant of local date/time components.
*
* - Compares all date and time components.
* - Returns false if either input is invalid.
*
* @param value1 first ISO PlainDateTime string
* @param value2 second ISO PlainDateTime string
* @returns boolean indicating whether both date-times are equal component-wise
*
* @example areDateTimesEqual("2024-02-29T12:34:56", "2024-02-29T12:34:56") // true
* @example areDateTimesEqual("2024-02-29T12:34:56", "2024-02-29T12:34:57") // false
* @example areDateTimesEqual("2024-02-29T12:34:56", "invalid") // false
*/
function areDateTimesEqual(value1, value2) {
const dateTime1 = plainDateTimeOf(value1);
const dateTime2 = plainDateTimeOf(value2);
if (dateTime1 === null || dateTime2 === null) return false;
try {
return dateTime1.year === dateTime2.year && dateTime1.month === dateTime2.month && dateTime1.day === dateTime2.day && dateTime1.hour === dateTime2.hour && dateTime1.minute === dateTime2.minute && dateTime1.second === dateTime2.second && dateTime1.millisecond === dateTime2.millisecond && dateTime1.microsecond === dateTime2.microsecond && dateTime1.nanosecond === dateTime2.nanosecond;
} catch {
return false;
}
}
export { areDateTimesEqual };