Parsing Date and Time
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
Section titled “Mistakes”Not handling the empty string on invalid input
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
const result = parseRfc2822(invalidInput); // ""save({ sentAt: result }); // stores an empty timestampRight
import { parseRfc2822 } from "@northguild/gmt/zoned";
const result = parseRfc2822(invalidInput);if (result === "") {// invalid RFC 2822 input}Checking a wire format with its regex
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
rfc3339DateTime.test("2023-02-29T10:00:00Z"); // true — but 2023 has no 29 FebruaryRight
import { isValidRfc3339DateTime } from "@northguild/gmt/zoned";
isValidRfc3339DateTime("2023-02-29T10:00:00Z"); // falseisValidRfc3339DateTime("2024-03-15T14:30:00-04:00"); // trueUsing string manipulation instead of parse functions
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
const [y, m, d] = dateStr.split("-"); // fragile — breaks on annotations or edge formatsRight
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
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
const m = parseMonthFromDate("2024-12-15"); // "12"const next = Number(m) + 1; // 13 — not a valid monthRight
import { addDate } from "@northguild/gmt";
addDate("2024-12-15", { months: 1 }); // "2025-01-15" — handles overflowConfusing getWeekOfMonth with parseWeekFromDate
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
const wom = getWeekOfMonth("2024-01-31", "en"); // 5// used as if it were an ISO week numberRight
import { getIsoWeekDate, parseWeekFromDate } from "@northguild/gmt";
const isoWeek = parseWeekFromDate("2024-01-31"); // 5getIsoWeekDate("2024-01-31"); // { year: 2024, week: 5, weekday: 3 } — year is the week-yearWeek number without week-year is a bug in waiting
'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
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 2025Right
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
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
const r = parseRfc3339(input);if (r === null) { /* never true — invalid input returns "" */ }Right
import { parseRfc3339 } from "@northguild/gmt/zoned";
const r = parseRfc3339(input);if (r === "") {// invalid input}Matching only the stand-alone month name
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
const MONTHS = ["январь", "февраль", "март", "апрель" /* ... */];const month = "15 марта 2024".split(" ")[1]; // "марта"MONTHS.includes(month); // false — the list has "март", not "марта"Right
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 yearReading a year-month with parseDateWithPattern
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
import { parseDateWithPattern } from "@northguild/gmt";
parseDateWithPattern("10/2026", "MM/yyyy"); // "" — no day to readRight
import { parseYearMonthWithPattern } from "@northguild/gmt";
parseYearMonthWithPattern("10/2026", "MM/yyyy"); // "2026-10"Trusting Temporal.PlainMonthDay.from with a day that never exists
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
const md = Temporal.PlainMonthDay.from("--02-30"); // no error in @js-temporal/polyfill 0.5.1md.toPlainDate({ year: 2024 }).toString(); // "2024-02-29" — a day nobody wroteRight
import { isValidMonthDay } from "@northguild/gmt";
isValidMonthDay("--02-30"); // falseSee also
Section titled “See also”© 2026 NorthGuild · Licences
Code examples: MIT-0. Packages: MIT. Third-party data and components: their own licences, credited where they appear. Everything else: © 2026 NorthGuild, all rights reserved.