Omit the year from dateToReadable within the current year

dateToReadable now formats current-year dates without the year (e.g.
"June 8th") and includes it otherwise (e.g. "February 3rd, 2024"). This
keeps the mention menu subtitle compact while the relative title shows
"Today"/"Tomorrow".
This commit is contained in:
Claude
2026-06-08 16:51:11 +00:00
parent 599fc6eb1c
commit 3446fe8ff7
2 changed files with 18 additions and 5 deletions
+10 -2
View File
@@ -19,8 +19,16 @@ describe("toISODate / parseISODate", () => {
});
describe("dateToReadable", () => {
it("formats an absolute, human-readable date", () => {
expect(dateToReadable("2024-02-03")).toBe("February 3rd, 2024");
it("includes the year outside the current year", () => {
expect(dateToReadable("2020-02-03")).toBe("February 3rd, 2020");
});
it("omits the year within the current year", () => {
const date = new Date();
date.setMonth(date.getMonth() === 0 ? 6 : 0);
date.setDate(15);
const result = dateToReadable(toISODate(date));
expect(result).not.toContain(`${date.getFullYear()}`);
});
it("returns the original string when invalid", () => {
+8 -3
View File
@@ -328,8 +328,9 @@ export function parseISODate(iso: string): Date | null {
/**
* Formats a date mention's stored ISO value into an absolute, localized,
* human-readable label (e.g. "January 2nd, 2024"). Suitable for plaintext
* and markdown serialization where a stable, unambiguous value is required.
* human-readable label. The year is omitted within the current year (e.g.
* "January 2nd") and included otherwise (e.g. "February 3rd, 2024"). Suitable
* for plaintext and markdown serialization.
*
* @param iso The date-only ISO string.
* @param language The user's language preference.
@@ -343,7 +344,11 @@ export function dateToReadable(
if (!date) {
return iso;
}
return format(date, "MMMM do, yyyy", { locale: dateLocale(language) });
const locale = dateLocale(language);
if (isSameYear(date, new Date())) {
return format(date, "MMMM do", { locale });
}
return format(date, "MMMM do, yyyy", { locale });
}
/**