# Parsing Date and Time

Industry: Core

GMT's parse functions accept ISO 8601 strings and common wire formats, returning
plain or zoned components. They never throw — invalid input yields an empty string
or `null`.

## Mistakes

### Not handling the empty string on invalid input

Severity: high.

parseRfc2822, parseHttpDate, and parseSqlDateTime return an empty string for invalid input and never throw. Passing the result on unchecked turns bad input into an empty value somewhere downstream.

Wrong:
```js
const result = parseRfc2822(invalidInput); // ""
save({ sentAt: result }); // stores an empty timestamp
```

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

const result = parseRfc2822(invalidInput);
if (result === "") {
  // invalid RFC 2822 input
}
```

### Checking a wire format with its regex

Severity: medium.

A pattern such as rfc3339DateTime proves the shape of a string, not that its date exists. It matches 29 February in a year that has none. The validator for the format is true exactly when the parser returns a value.

Wrong:
```js
rfc3339DateTime.test("2023-02-29T10:00:00Z"); // true — but 2023 has no 29 February
```

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

isValidRfc3339DateTime("2023-02-29T10:00:00Z"); // false
isValidRfc3339DateTime("2024-03-15T14:30:00-04:00"); // true
```

### Using string manipulation instead of parse functions

Severity: medium.

Splitting a date string on '-' and indexing the parts works for ISO 8601 today, but it breaks the moment your input includes a calendar annotation, a different separator, or fractional seconds.

Wrong:
```js
const [y, m, d] = dateStr.split("-"); // fragile — breaks on annotations or edge formats
```

Right:
```js
import { parseYearFromDate, parseMonthFromDate, parseDayFromDate } from "@northguild/gmt";

const y = parseYearFromDate(dateStr);
const m = parseMonthFromDate(dateStr);
const d = parseDayFromDate(dateStr);
```

### Assuming month is 1-indexed in arithmetic

Severity: medium.

parseMonthFromDate returns the 1-indexed month as a two-digit string, '01' to '12'. Adding or subtracting months on that number without a date-aware function can produce invalid month values.

Wrong:
```js
const m = parseMonthFromDate("2024-12-15"); // "12"
const next = Number(m) + 1; // 13 — not a valid month
```

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

addDate("2024-12-15", { months: 1 }); // "2025-01-15" — handles overflow
```

### Confusing getWeekOfMonth with parseWeekFromDate

Severity: medium.

getWeekOfMonth tells you which week-of-month a date falls in (1-6). parseWeekFromDate returns the ISO week number (1-53). They answer different questions and produce different numbers for the same input.

Wrong:
```js
const wom = getWeekOfMonth("2024-01-31", "en"); // 5
// used as if it were an ISO week number
```

Right:
```js
import { getIsoWeekDate, parseWeekFromDate } from "@northguild/gmt";

const isoWeek = parseWeekFromDate("2024-01-31"); // 5
getIsoWeekDate("2024-01-31"); // { year: 2024, week: 5, weekday: 3 } — year is the week-year
```

### Week number without week-year is a bug in waiting

Severity: medium.

'Wait until week 35' is ambiguous without a week-year. ISO week 35 of 2024 and week 35 of 2025 are 52 weeks apart. Always pair the week number with its week-year.

Wrong:
```js
const week = parseWeekFromDate(startDate); // 35
// wait until week 35 — but which year?
parseYearFromDate("2024-12-30"); // "2024" — the calendar year, but that date is in week 1 of 2025
```

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

getIsoWeekDate("2024-12-30"); // { year: 2025, week: 1, weekday: 1 }
const { year: weekYear, week } = getIsoWeekDate(startDate);
// wait until week <week> of <weekYear>
```

### Checking for null on parseRfc2822/parseHttpDate/parseSqlDateTime/parseRfc3339

Severity: medium.

These functions return an empty string for invalid input, never null. A null check never fires, so invalid input passes straight through it. Check for the empty string.

Wrong:
```js
const r = parseRfc3339(input);
if (r === null) { /* never true — invalid input returns "" */ }
```

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

const r = parseRfc3339(input);
if (r === "") {
  // invalid input
}
```

### Matching only the stand-alone month name

Severity: medium.

Some languages inflect a month name when a day comes with it. A hand-written list of the stand-alone names misses the inflected form, so '15 марта 2024' fails while '15 март 2024' passes. parseDateWithPattern reads both.

Wrong:
```js
const MONTHS = ["январь", "февраль", "март", "апрель" /* ... */];
const month = "15 марта 2024".split(" ")[1]; // "марта"
MONTHS.includes(month); // false — the list has "март", not "марта"
```

Right:
```js
import { parseDateWithPattern, parseMonthDayWithPattern } from "@northguild/gmt";

parseDateWithPattern("15 марта 2024", "d MMMM yyyy", "ru-RU"); // "2024-03-15"
parseMonthDayWithPattern("25 декабря", "d MMMM", "ru-RU"); // "--12-25" — for a value with no year
```

### Reading a year-month with parseDateWithPattern

Severity: medium.

A date needs a day, so parseDateWithPattern returns an empty string for a pattern with none. Appending a day to make it parse invents a day the producer never sent. parseYearMonthWithPattern reads the year and month alone.

Wrong:
```js
import { parseDateWithPattern } from "@northguild/gmt";

parseDateWithPattern("10/2026", "MM/yyyy"); // "" — no day to read
```

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

parseYearMonthWithPattern("10/2026", "MM/yyyy"); // "2026-10"
```

### Trusting Temporal.PlainMonthDay.from with a day that never exists

Severity: medium.

The grammar refuses --02-30, and temporal-polyfill 1.0.9 throws on it, but @js-temporal/polyfill 0.5.1 accepted it and kept day 30, so placing it in a year quietly gave 29 February. isValidMonthDay refuses it first, whichever polyfill runs.

Wrong:
```js
const md = Temporal.PlainMonthDay.from("--02-30"); // no error in @js-temporal/polyfill 0.5.1
md.toPlainDate({ year: 2024 }).toString(); // "2024-02-29" — a day nobody wrote
```

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

isValidMonthDay("--02-30"); // false
```

## See also

- [Parsing guide](/gmt/guides/core-date-operations/parsing/)
- [`parseRfc2822` reference](/gmt/reference/zoned/parse/parseRfc2822/)
- [`parseRfc3339` reference](/gmt/reference/zoned/parse/parseRfc3339/)
- [`isValidRfc3339DateTime` reference](/gmt/reference/zoned/validate/isValidRfc3339DateTime/)
- [`parseYearFromDate` reference](/gmt/reference/plain/parse/parseYearFromDate/)
- [`parseWeekFromDate` reference](/gmt/reference/plain/parse/parseWeekFromDate/)
- [`getIsoWeekDate` reference](/gmt/reference/calendar/calculate/getIsoWeekDate/)
