Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(utils): humane units for distant calendar dates in the popover header

Sidwebworksbc8d29cb96d1
diff --git a/packages/utils/src/date.test.ts b/packages/utils/src/date.test.ts
index dc4ac999fd..d914deef60 100644
--- a/packages/utils/src/date.test.ts
+++ b/packages/utils/src/date.test.ts
@@ -29,6 +29,19 @@ describe("formatRelativeCalendarDays", () => {
).toBe("yesterday");
});
+ it("switches to months and years for distant dates", () => {
+ expect(
+ formatRelativeCalendarDays("2026-11-10", "2026-08-06", "en-US")
+ ).toBe("in 3 months");
+ // 589 days back — never "589 days ago"
+ expect(
+ formatRelativeCalendarDays("2024-12-25", "2026-08-06", "en-US")
+ ).toBe("2 years ago");
+ expect(
+ formatRelativeCalendarDays("2025-09-06", "2026-08-06", "en-US")
+ ).toBe("11 months ago");
+ });
+
it("echoes garbage input", () => {
expect(formatRelativeCalendarDays("nope", "2026-08-06", "en-US")).toBe(
"nope"
diff --git a/packages/utils/src/date.ts b/packages/utils/src/date.ts
index 8dfe16f793..4f8f5e5957 100644
--- a/packages/utils/src/date.ts
+++ b/packages/utils/src/date.ts
@@ -169,10 +169,12 @@ export function getTimeZoneOffsetLabel(isoString: string, timeZone: string) {
}
}
-// Exact day-level relative distance between two `YYYY-MM-DD` calendar days —
-// "in 8 days" / "3 days ago", with "today"/"tomorrow"/"yesterday" for the near
-// cases. The caller supplies `todayString` on whichever calendar the date
-// belongs to (company/location), so the flip happens at business midnight.
+// Relative distance between two `YYYY-MM-DD` calendar days, in the most
+// humane unit: exact days near the present ("in 8 days", with "today"/
+// "tomorrow"/"yesterday"), months beyond ~a month, years beyond ~a year —
+// never "589 days ago". The caller supplies `todayString` on whichever
+// calendar the date belongs to (company/location), so the flip happens at
+// business midnight.
export function formatRelativeCalendarDays(
dateString: string,
todayString: string,
@@ -184,7 +186,11 @@ export function formatRelativeCalendarDays(
parseDate(todayString).toDate("UTC").getTime()) /
86400000
);
- return getRelativeFormatter(locale || DEFAULT_LOCALE).format(days, "days");
+ const formatter = getRelativeFormatter(locale || DEFAULT_LOCALE);
+ if (Math.abs(days) < 30) return formatter.format(days, "days");
+ if (Math.abs(days) < 365)
+ return formatter.format(Math.round(days / 30.44), "months");
+ return formatter.format(Math.round(days / 365.25), "years");
} catch {
return dateString;
}