Skip to content

areDateTimesEqualBy

areDateTimesEqualBy(value1: string, value2: string, unit: any, optionsArg?: { weekStartsOn?: "monday" | "sunday"; fractionalSecondDigits?: FractionalDigit; }): boolean
import { areDateTimesEqualBy } from "@northguild/gmt/plain/compare";

Compare two ISO datetime strings for equality at a given unit.

  • Equality means both values share the same start-of-unit boundary, so "month" requires the same month AND year — March 2023 and March 2024 are NOT equal by month, matching date-fns’s isSameMonth and Luxon’s dt.hasSame(other, "month").
  • Supports the full Temporal.DateUnit \| Temporal.TimeUnit range, down to "nanosecond".
  • Returns false for an unsupported unit or invalid input. Mapping from date-fns (Decision 5, context/roadmap/issues/J.md):
  • isSameDay(a, b)areDateTimesEqualBy(a, b, "day")
  • isSameHour(a, b)areDateTimesEqualBy(a, b, "hour")
  • isSameMinute(a, b)areDateTimesEqualBy(a, b, "minute")
  • isSameSecond(a, b)areDateTimesEqualBy(a, b, "second")
  • isSameMonth(a, b)areDateTimesEqualBy(a, b, "month")
  • isSameYear(a, b)areDateTimesEqualBy(a, b, "year")
Parameter Type Description
value1 string first ISO datetime string
value2 string second ISO datetime string
unit any Temporal.DateUnit | Temporal.TimeUnit to compare by

options

Option Type Default
weekStartsOn? "monday" | "sunday"
fractionalSecondDigits? FractionalDigit

true if both datetimes share the same start-of-unit boundary, false on an unsupported unit or invalid input

areDateTimesEqualBy("2024-03-15T10:00:00", "2024-03-15T18:00:00", "day") // true
areDateTimesEqualBy("2024-03-15T10:30:00", "2024-03-15T10:45:00", "hour") // true
areDateTimesEqualBy("2024-03-15T10:30:00", "2024-03-15T11:00:00", "hour") // false
areDateTimesEqualBy("2023-03-15T10:00:00", "2024-03-15T10:00:00", "month") // false (same month, different year)
areDateTimesEqualBy("invalid", "2024-03-15T10:00:00", "day") // false

plain/compare/areDateTimesEqualBy.ts