Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/utilities/src/dateTimeFormat/relative-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ describe(locale, () => {
expect(actualOutput).toBe(rtf.format(1, 'weeks'));
});

test(`-364 days → ${rtf.format(-12, 'months')}`, () => {
const date = Date.now() - 1000 * 60 * 60 * 24 * 364;
const actualOutput = relative.format(date, { locale, style });
expect(actualOutput).toBe(rtf.format(-12, 'months'));
});

test(`+364 days → ${rtf.format(12, 'months')}`, () => {
const date = Date.now() + 1000 * 60 * 60 * 24 * 364;
const actualOutput = relative.format(date, { locale, style });
expect(actualOutput).toBe(rtf.format(12, 'months'));
});

tests.forEach(([unit, unitsPassed, secondsInUnit]) => {
const datePast = Date.now() - 1000 * unitsPassed * secondsInUnit;
const expectedPast = rtf.format(unitsPassed * -1, unit);
Expand Down
4 changes: 3 additions & 1 deletion packages/utilities/src/dateTimeFormat/relative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export function format(
const hours = Math.trunc(minutes / 60);
const days = Math.trunc(hours / 24);
const weeks = Math.trunc(days / 7);
const months = Math.trunc(weeks / 4);
// A month is counted as 4 weeks, which reaches 13 just before a full year,
// so cap it at 12
const months = Math.max(-12, Math.min(12, Math.trunc(weeks / 4)));
const years = Math.trunc(days / 365);

if (Math.abs(seconds) < 60) {
Expand Down
Loading