# Formatting Relative Time

Industry: Core

`formatRelativeDate`, `formatRelativeTime`, and `formatRelativeDateTime` produce
human-readable descriptions like "3 days ago" or "in 2 hours". They require both
a value and a reference point.

## Mistakes

### Forgetting reference where the output must be deterministic

Severity: high.

Without a reference, formatRelativeDate and friends measure from now: the system clock and, for plain values, the system time zone. The same call returns a different string tomorrow. Pass reference wherever the output is tested, cached or rendered on a server. It goes in the options object; the second argument is the locale.

Wrong:
```js
formatRelativeDate("2024-03-15", "en"); // measured from today — changes from day to day
```

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

formatRelativeDate("2024-03-15", "en", { reference: "2024-03-20" }); // "5 days ago"
```

### Mismatched input shape between value and reference

Severity: high.

A plain date and a zoned datetime are different shapes. Passing a plain date as the value and a zoned string as the reference (or vice versa) returns an empty string.

Wrong:
```js
formatRelativeDate("2024-03-15", "en", { reference: "2024-03-20T10:00:00+00:00[Europe/London]" });
// "" — plain value, zoned reference
```

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

formatRelativeDate("2024-03-15", "en", { reference: "2024-03-20" }); // "5 days ago" — both plain
```

### Hand-rolling diff math with Date

Severity: high.

Computing '3 days ago' by dividing a millisecond difference by 86400000 assumes every day is 24 hours, and new Date(target) reads a date-only string as UTC midnight, so the count shifts with the time of day, the host time zone and DST changes. GMT's formatRelative functions count calendar days.

Wrong:
```js
const diff = Math.floor((Date.now() - new Date(target).getTime()) / 86400000);
// "3 days" — off by one depending on the hour, the host zone, or a DST change in between
```

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

formatRelativeDate("2024-03-29", "en", { reference: "2024-04-01" }); // "3 days ago" — calendar days, across the 2024-03-31 DST change
```

### Picking the wrong formatter for a Unix value

Severity: medium.

formatRelativeUnix is the formatter for Unix timestamps. Passing a Unix value to formatRelativeDate or formatRelativeDateTime returns an empty string because the input shape does not match.

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

formatRelativeDate(1710504645000, "en"); // "" — an epoch number is not a plain date
```

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

formatRelativeUnix(1710504645000, "en", { reference: 1710504645000 }); // "now" — milliseconds by default
```

## See also

- [Relative Time guide](/gmt/guides/core-date-operations/relative-time/)
- [`formatRelativeDate` reference](/gmt/reference/plain/format/formatRelativeDate/)
- [`formatRelativeTime` reference](/gmt/reference/plain/format/formatRelativeTime/)
- [`formatRelativeUnix` reference](/gmt/reference/unix/format/formatRelativeUnix/)
- [`formatRelativeZoned` reference](/gmt/reference/zoned/format/formatRelativeZoned/)
