areDatesEqualBy
Signature
Section titled “Signature”areDatesEqualBy(value1: string, value2: string, unit: SmallestUnit<DateUnit>, optionsArg?: { weekStartsOn?: "monday" | "sunday"; }): booleanCompare two ISO date strings for equality at a given calendar unit.
- Equality means both values share the same start-of-
unitboundary, so"month"requires the same month AND year — March 2023 and March 2024 are NOT equal by month. A calendar month is a year and a month together, as in Temporal’sPlainYearMonth, not a bare month-of-year number. This is a common point of confusion for callers expecting a bare “same month-of-year” comparison. - Comparison: date-fns’s
isSameMonthand Luxon’sdt.hasSame(other, "month")give the same answer. "day"matchesareDatesEqualfor PlainDate inputs only:areDatesEqualalso accepts PlainDateTime strings (areDatesEqual("2024-01-01", "2024-01-01T10:00")istrue), while this function accepts PlainDate strings only (areDatesEqualBywith those inputs and"day"isfalse)."year","month","week"are computed viastartOfDate.unitaccepts the singular or plural name ("day"or"days"), as Temporal does.- Returns false for an unsupported unit or invalid input. Mapping from date-fns:
isSameDay(a, b)→areDatesEqualBy(a, b, "day")isSameWeek(a, b, options)→areDatesEqualBy(a, b, "week", { weekStartsOn })isSameMonth(a, b)→areDatesEqualBy(a, b, "month")isSameYear(a, b)→areDatesEqualBy(a, b, "year")
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value1 | string | first ISO date string |
value2 | string | second ISO date string |
unit | SmallestUnit<DateUnit> | date unit to compare by (“year” | “month” | “week” | “day”, or its plural) |
Options
Section titled “Options”optionsArg
How the week is defined
| Option | Type | Default | Description |
|---|---|---|---|
weekStartsOn? | "monday" | "sunday" | "monday" | The first day of the week, which sets where a "week" unit starts. "monday" is the ISO 8601 week, Monday to Sunday; "sunday" runs Sunday to Saturday. Any other value returns false, whatever the unit. |
Returns
Section titled “Returns”true if both dates share the same start-of-unit boundary, false on an unsupported unit or invalid input
Examples
Section titled “Examples”import { areDatesEqualBy } from "@northguild/gmt/plain/compare";
areDatesEqualBy("2024-03-15", "2024-03-20", "month") // true