# Date Comparison

Industry: Core

Comparison functions in GMT are string-in, string-out. They never throw — invalid
input yields `false`. Knowing what `false` means is half the battle.

## Mistakes

### Using string comparison for dates

Severity: high.

Lexicographic string comparison ('2024-03-01' < '2024-02-15') works for ISO 8601 dates but breaks the moment your input includes timezones, offsets, or calendar annotations. Use isBeforeDate and isAfterDate instead.

Wrong:
```js
"2024-03-01" < "2024-02-15"; // false — string order is not date order for all formats
```

Right:
```js
import { isBeforeDate } from "@northguild/gmt";

isBeforeDate("2024-02-15", "2024-03-01"); // true — correct date comparison
```

### Not handling invalid input

Severity: medium.

All comparison functions return false for invalid input. A false result means either 'not the case' or 'invalid input' — you cannot tell which without checking the inputs first.

Wrong:
```js
const r = isBeforeDate(badInput, "2024-03-15");
// r is false — but was badInput invalid, or is it genuinely not before?
```

Right:
```js
import { isValidDate, isBeforeDate } from "@northguild/gmt";

if (isValidDate(a) && isValidDate(b)) {
  isBeforeDate(a, b); // now false means genuinely not before
}
```

### Comparing different date formats

Severity: medium.

isBeforeDate compares two plain dates. A zoned datetime string is invalid input to it, so the result is false whatever the dates are. Convert first so both inputs have the same shape.

Wrong:
```js
isBeforeDate("2024-03-14", "2024-03-15T10:00:00+09:00[Asia/Tokyo]"); // false — the zoned string is invalid input
```

Right:
```js
import { isBeforeDate, parseDateFromZoned } from "@northguild/gmt";

isBeforeDate("2024-03-14", parseDateFromZoned("2024-03-15T10:00:00+09:00[Asia/Tokyo]"));
// true — both are plain dates
```

### Assuming weekends are always Saturday/Sunday

Severity: medium.

isWeekend follows the locale's weekend definition. In Israel, the weekend is Friday-Saturday. In some Gulf states, it is Friday-Saturday or Saturday-Sunday depending on recent policy changes.

Wrong:
```js
const isWeekend = (d) => {
  const day = new Date(d).getDay();
  return day === 0 || day === 6; // Saturday/Sunday only — wrong for many locales
};
```

Right:
```js
import { isWeekend } from "@northguild/gmt";

isWeekend("2024-03-15", "en"); // false
isWeekend("2024-03-15", "he-IL"); // true — 2024-03-15 is a Friday, and he-IL's weekend is Friday–Saturday
```

### Confusing isBusinessDay with isWeekend

Severity: medium.

isWeekend follows the locale's weekend. isBusinessDay takes no locale: it defaults to Monday–Friday with no holidays, or takes a business calendar with its own weekend and holidays. So !isWeekend(d, locale) and isBusinessDay(d) disagree wherever the locale's weekend is not Saturday–Sunday, and on every holiday.

Wrong:
```js
// assuming !isWeekend(d, locale) === isBusinessDay(d)
```

Right:
```js
import { isWeekend, isBusinessDay } from "@northguild/gmt";

isWeekend("2024-03-15", "he-IL"); // true — Friday
isBusinessDay("2024-03-15"); // true — the default calendar is Monday–Friday
isBusinessDay("2024-03-15", { weekend: [5, 6], holidays: [], timeZone: "Asia/Jerusalem" }); // false
```

### Assuming 'same month' ignores the year

Severity: medium.

'Same month' means same month and same year. March 2024 and March 2025 are different months. areDatesEqualBy with 'month' compares both the month and the year.

Wrong:
```js
areDatesEqualBy("2024-03-15", "2025-03-15", "month"); // false — different years
```

Right:
```js
import { areDatesEqualBy } from "@northguild/gmt";

areDatesEqualBy("2024-03-15", "2024-03-20", "month"); // true — same month and year
```

### Assuming isRelativeDay/isThisUnit/isPast/isFuture are timeZone-independent

Severity: high.

The plain predicates resolve 'today' in the system time zone, and a plain date has no zone to pass. A date that is 'today' in São Paulo is 'tomorrow' in Tokyo. When the result must not depend on the host, use the zoned variants: isZonedRelativeDay and isZonedThisUnit resolve 'today' in the value's own time zone, and isZonedPast and isZonedFuture compare instants.

Wrong:
```js
isPast("2024-03-15"); // depends on the system timezone at runtime
```

Right:
```js
import { isZonedPast } from "@northguild/gmt";

isZonedPast("2024-03-15T00:00:00+11:00[Australia/Sydney]"); // compares instants — no host time zone involved
```

### Assuming nextWeekday/previousWeekday return the input when it's already on the target day

Severity: medium.

nextWeekday on a Friday with target day 5 (Friday) returns the *next* Friday, not the input. For 'the next occurrence, including this day', pass { inclusive: true }.

Wrong:
```js
nextWeekday("2024-03-15", 5); // "2024-03-22" — even though it's already Friday
```

Right:
```js
import { nextWeekday } from "@northguild/gmt";

nextWeekday("2024-03-15", 5, { inclusive: true }); // "2024-03-15" — already a Friday
```

## See also

- [Comparisons guide](/gmt/guides/core-date-operations/comparisons/)
- [`isBeforeDate` reference](/gmt/reference/plain/compare/isBeforeDate/)
- [`isAfterDate` reference](/gmt/reference/plain/compare/isAfterDate/)
- [`isWeekend` reference](/gmt/reference/plain/compare/isWeekend/)
- [`isPast` reference](/gmt/reference/plain/compare/isPast/)
- [`isFuture` reference](/gmt/reference/plain/compare/isFuture/)
- [`nextWeekday` reference](/gmt/reference/plain/calculate/nextWeekday/)
- [`areDatesEqualBy` reference](/gmt/reference/plain/compare/areDatesEqualBy/)
