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(table): render date columns in the formatting locale, closes #10703
Elian Doran00831ab2cbf2
diff --git a/apps/client/src/widgets/collections/table/columns.spec.ts b/apps/client/src/widgets/collections/table/columns.spec.tsindex 9745f28496..8624fd27eb 100644--- a/apps/client/src/widgets/collections/table/columns.spec.ts+++ b/apps/client/src/widgets/collections/table/columns.spec.ts@@ -1,7 +1,9 @@import { describe, expect, it } from "vitest";-import { restoreExistingData } from "./columns";+import { formatLabelDate, restoreExistingData } from "./columns";import type { ColumnDefinition } from "tabulator-tables";+import options from "../../../services/options";+describe("restoreExistingData", () => {it("maintains important columns properties", () => {const newDefs: ColumnDefinition[] = [@@ -131,3 +133,33 @@ describe("restoreExistingData", () => {expect(restored[0].width).toStrictEqual("100px");});});++describe("formatLabelDate", () => {+ it("renders dates and datetimes in the configured formatting locale", () => {+ options.set("formattingLocale", "de");++ expect(formatLabelDate("2026-01-31", "none")).toBe("31.01.26");+ expect(formatLabelDate("2026-01-31T14:05", "short")).toBe("31.01.26, 14:05");+ });++ it("keeps a date-only value on its calendar day rather than shifting it by timezone", () => {+ options.set("formattingLocale", "de");++ // Parsed as UTC midnight, "2026-01-31" would roll back a day in a negative UTC offset.+ expect(formatLabelDate("2026-01-31", "none")).toBe("31.01.26");+ });++ it("echoes values that are not dates instead of throwing inside the grid render", () => {+ options.set("formattingLocale", "de");++ // Intl throws a RangeError on an invalid date, which would break the whole table.+ expect(formatLabelDate("not a date", "short")).toBe("not a date");+ expect(formatLabelDate("not a date", "none")).toBe("not a date");+ });++ it("renders a blank cell for missing values", () => {+ expect(formatLabelDate(undefined, "none")).toBe("");+ expect(formatLabelDate(null, "short")).toBe("");+ expect(formatLabelDate("", "none")).toBe("");+ });+});diff --git a/apps/client/src/widgets/collections/table/columns.tsx b/apps/client/src/widgets/collections/table/columns.tsxindex 3dc92e8958..fe40aff133 100644--- a/apps/client/src/widgets/collections/table/columns.tsx+++ b/apps/client/src/widgets/collections/table/columns.tsx@@ -4,6 +4,7 @@ import { useEffect, useRef, useState } from "preact/hooks";import type { CellComponent, ColumnDefinition, EmptyCallback, FormatterParams, RowComponent, ValueBooleanCallback, ValueVoidCallback } from "tabulator-tables";import froca from "../../../services/froca.js";+import { formatDateTime } from "../../../utils/formatters.js";import Icon from "../../react/Icon.jsx";import NoteAutocomplete from "../../react/NoteAutocomplete.jsx";import { renderReactWidget } from "../../react/react_utils.jsx";@@ -37,9 +38,12 @@ const labelTypeMappings: Record<ColumnType, Partial<ColumnDefinition>> = {},date: {editor: "date",+ // Values are stored as ISO ("YYYY-MM-DD"), which Tabulator would otherwise render as-is.+ formatter: (cell) => formatLabelDate(cell.getValue(), "none")},datetime: {- editor: "datetime"+ editor: "datetime",+ formatter: (cell) => formatLabelDate(cell.getValue(), "short")},number: {editor: "number",@@ -67,6 +71,22 @@ const labelTypeMappings: Record<ColumnType, Partial<ColumnDefinition>> = {}};+/**+ * Renders a stored date label through the user's formatting locale.+ *+ * Unparseable values are echoed back rather than formatted: label values are free text, so a+ * date-typed column can hold anything a user typed, imported, or left behind by retyping a text+ * label as a date. `Intl.DateTimeFormat` throws a `RangeError` on an invalid date, and since this+ * runs inside a Tabulator formatter, that would take down the whole grid rather than one cell.+ */+export function formatLabelDate(value: unknown, timeStyle: "none" | "short") {+ if (typeof value !== "string" || !value) return "";+ // Passed as a string so that formatDateTime() keeps its date-only ("YYYY-MM-DD") handling,+ // which pins the value to the local calendar day instead of shifting it across timezones.+ if (Number.isNaN(new Date(value).getTime())) return value;+ return formatDateTime(value, "short", timeStyle);+}+interface BuildColumnArgs {info: AttributeDefinitionInformation[];movableRows: boolean;