# Formatting Date and Time

Industry: Core

GMT's format functions return locale-sensitive strings. They differ from
`Intl.DateTimeFormat` in scope, locale handling, and sentinel behavior.

## Mistakes

### Using Intl.DateTimeFormat directly

Severity: high.

Intl.DateTimeFormat does not cover GMT's extended format set (Rfc2822, HTTP, SQL, Rfc3339, relative), and the order of its formatToParts output varies by locale. GMT's format functions are consistent across locales and formats.

Wrong:
```js
const f = new Intl.DateTimeFormat("en", { dateStyle: "full" });
f.format(new Date("2024-03-15")); // "Thursday, March 14, 2024" in São Paulo — the Date is UTC midnight
```

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

formatDate("2024-03-15", "en", { dateStyle: "full" }); // "Friday, March 15, 2024" — no time zone involved
```

### Not handling empty string on invalid input

Severity: medium.

GMT's format functions return an empty string for invalid input. Treating that as a blank format result hides bugs in the input pipeline.

Wrong:
```js
const out = formatDate(badInput);
// if badInput is "", out is also "" — indistinguishable from a formatting bug
```

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

const out = formatDate(input);
if (out === "") {
  // input was invalid — fix upstream
}
```

### Assuming getLocale* rejects a locale it has no data for

Severity: medium.

getLocaleMonthNames, getLocaleEraNames, and friends return an empty array only for a malformed locale tag. A well-formed tag with no locale data is not an error: it falls back to the runtime's default locale, as ECMA-402 locale resolution requires. They never throw.

Wrong:
```js
const months = getLocaleMonthNames("xx-XX");
if (months.length === 0) {
  // never reached — "xx-XX" is well-formed, so this is the default locale's names
}
```

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

getLocaleMonthNames("not-a-locale-!!"); // [] — only a malformed tag is rejected
// check for locale data yourself, e.g. Intl.DateTimeFormat.supportedLocalesOf(["xx-XX"]) is []
```

### Looking for a token-pattern formatter (there isn't one, deliberately)

Severity: medium.

GMT deliberately omits a format(pattern, value) function. Token-pattern formatters either silently ignore unknown tokens or produce garbled output on bad input. GMT's dedicated format functions are safer.

Wrong:
```js
// no equivalent of format("{yyyy}-{MM}-{dd}", date)
```

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

formatDate("2024-03-15", "en", { dateStyle: "long" }); // "March 15, 2024" — dedicated function, no tokens
```

### Reassembling formatToParts output in a fixed order

Severity: medium.

Intl.DateTimeFormat.formatToParts() returns parts in locale-specific order. Joining them in a fixed sequence (month before day, for example) gives wrong output for ja-JP, zh-CN and other locales.

Wrong:
```js
const parts = new Intl.DateTimeFormat("ja-JP").formatToParts(new Date());
// parts[0] is the year, not the month — hardcoding en-US order breaks here
```

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

formatDate("2024-03-15", "ja-JP", { dateStyle: "long" }); // "2024年3月15日" — locale-correct order, no manual assembly
```

### Confusing formatCalendar with formatRelativeDateTime

Severity: medium.

formatCalendar names the calendar day and the time of a datetime — 'tomorrow at 9:00 AM' — and falls back to an absolute date a week or more away. formatRelativeDateTime states a distance — 'tomorrow', 'in 3 hours', '3 days ago' — with no time of day. They have different output shapes.

Wrong:
```js
formatCalendar("2024-03-16T09:00:00", "en", { reference: "2024-03-15T08:00:00" });
// "tomorrow at 9:00 AM" — used where a relative distance was wanted
```

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

formatRelativeDateTime("2024-03-16T09:00:00", "en", { reference: "2024-03-15T08:00:00" }); // "tomorrow"
```

### Calling toLocaleString on an ISO month-day

Severity: high.

Temporal.PlainMonthDay has the ISO calendar, and toLocaleString throws a RangeError when the locale's calendar differs, which it does for en-US. formatMonthDay formats through the Gregorian calendar and returns text, or an empty string for invalid input.

Wrong:
```js
Temporal.PlainMonthDay.from("--12-25").toLocaleString("en-US");
// throws a RangeError
```

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

formatMonthDay("--12-25", "en-US"); // "12/25"
```

### Calling toLocaleString on an ISO year-month

Severity: high.

Temporal.PlainYearMonth has the ISO calendar, and toLocaleString throws a RangeError when the locale's calendar differs, which it does for en-US. This throws in temporal-polyfill 1.0.9, in Chromium 153 and in WebKit 26.6. formatYearMonth formats through the Gregorian calendar and returns text, or an empty string for invalid input.

Wrong:
```js
Temporal.PlainYearMonth.from("2026-10").toLocaleString("en-US");
// throws a RangeError
```

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

formatYearMonth("2026-10", "en-US"); // "10/2026"
```

## See also

- [Formatting guide](/gmt/guides/core-date-operations/formatting/)
- [`formatDate` reference](/gmt/reference/plain/format/formatDate/)
- [`formatRelativeDate` reference](/gmt/reference/plain/format/formatRelativeDate/)
- [`formatRelativeDateTime` reference](/gmt/reference/plain/format/formatRelativeDateTime/)
- [`getLocaleMonthNames` reference](/gmt/reference/plain/locale/getLocaleMonthNames/)
- [`formatRfc3339` reference](/gmt/reference/zoned/format/formatRfc3339/)
- [`formatRfc2822` reference](/gmt/reference/zoned/format/formatRfc2822/)
