Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(recent-changes): show correct date header for negative UTC offsets (closes #8497)
Elian Doranc7e3d4261b5c
diff --git a/apps/client/src/utils/formatters.spec.ts b/apps/client/src/utils/formatters.spec.tsindex 90f4e22cea..a0b3324196 100644--- a/apps/client/src/utils/formatters.spec.ts+++ b/apps/client/src/utils/formatters.spec.ts@@ -49,6 +49,22 @@ describe("formatters", () => {expect(formatDateTime(new Date(), "none", "full")).toBeTruthy();});+ it("renders a date-only string as the same calendar day in any timezone", () => {+ // Regression for #8497: a "YYYY-MM-DD" string was parsed as UTC midnight by the+ // Date constructor, which rolls back to the previous day for negative UTC+ // offsets (e.g. the Recent Changes date headers showed yesterday for a UTC-8+ // user). It must be treated as a local calendar date instead.+ options.set("formattingLocale", "en-US");++ // The date header itself must match the local calendar day, not a UTC shift.+ expect(formatDateTime("2026-01-25", "full", "none")).toBe(new Date(2026, 0, 25).toLocaleDateString("en-US", { dateStyle: "full" }));++ // Timezone-independent guard: a correctly parsed date-only string is *local*+ // midnight in any timezone, so its time renders as 00:00. The buggy UTC parse+ // produced a non-midnight local time in every zone east/west of UTC.+ expect(formatDateTime("2026-01-25", "none", "short")).toBe("12:00 AM");+ });+it("falls back to the locale option then navigator.language", () => {// Empty formattingLocale forces the `|| options.get("locale")` branch.options.set("formattingLocale", "");diff --git a/apps/client/src/utils/formatters.ts b/apps/client/src/utils/formatters.tsindex a7c9cceed5..f9d5ab234f 100644--- a/apps/client/src/utils/formatters.ts+++ b/apps/client/src/utils/formatters.ts@@ -12,10 +12,20 @@ export function formatDateTime(date: string | Date | number | null | undefined,const locale = normalizeLocale(options.get("formattingLocale") || options.get("locale") || navigator.language);- let parsedDate;+ let parsedDate: Date;if (typeof date === "string" || typeof date === "number") {- // Parse the given string as a date- parsedDate = new Date(date);+ const dateOnlyMatch = typeof date === "string" && /^(\d{4})-(\d{2})-(\d{2})$/.exec(date);+ if (dateOnlyMatch) {+ // A date-only string ("YYYY-MM-DD") is parsed as UTC midnight by the Date+ // constructor, so it rolls back to the previous day when displayed in a+ // negative UTC offset (e.g. the Recent Changes date headers). Treat it as a+ // local calendar date so the same day is shown regardless of timezone.+ const [ , year, month, day ] = dateOnlyMatch;+ parsedDate = new Date(Number(year), Number(month) - 1, Number(day));+ } else {+ // Parse the given string as a date+ parsedDate = new Date(date);+ }} else if (date instanceof Date) {// The given date is already a Date instance or a numberparsedDate = date;