Skip to content

Build your personal knowledge base with Trilium Notes

feat(calendar): show how long a timed event lasts

Elian Doran83908ab65c4a
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 4f548c377d..03f62dcf5e 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3682,7 +3682,10 @@
"minutes_one": "{{count}} minute",
"minutes_other": "{{count}} minutes",
"hours_one": "{{count}} hour",
- "hours_other": "{{count}} hours"
+ "hours_other": "{{count}} hours",
+ "duration_minutes": "{{minutes}} min",
+ "duration_hours": "{{hours}} h",
+ "duration_hours_minutes": "{{hours}} h {{minutes}} min"
},
"ocr": {
"extracted_text": "Extracted Text (OCR)",
diff --git a/apps/client/src/widgets/collections/calendar/EventDatesEditor.spec.tsx b/apps/client/src/widgets/collections/calendar/EventDatesEditor.spec.tsx
index ec82af31a5..03c1a86bb5 100644
--- a/apps/client/src/widgets/collections/calendar/EventDatesEditor.spec.tsx
+++ b/apps/client/src/widgets/collections/calendar/EventDatesEditor.spec.tsx
@@ -11,6 +11,12 @@ import { buildNote } from "../../../test/easy-froca";
import { ParentComponent } from "../../react/react_utils";
import EventDatesEditor from "./EventDatesEditor";
+// i18next is never initialized under test, so t() renders the key with the interpolated values
+// appended, e.g. "calendar_view.duration_hours_minutes|1|30".
+vi.mock("../../../services/i18n", () => ({
+ t: (key: string, opts?: Record<string, unknown>) => [ key, ...Object.values(opts ?? {}) ].join("|")
+}));
+
describe("EventDatesEditor", () => {
let container: HTMLElement;
const setLabel = vi.spyOn(attributes, "setLabel").mockResolvedValue(undefined);
@@ -39,6 +45,7 @@ describe("EventDatesEditor", () => {
const allDayBox = () => container.querySelector<HTMLInputElement>("input[type='checkbox']");
const dateInputs = () => container.querySelectorAll<HTMLInputElement>("input[type='date']");
const timeInputs = () => container.querySelectorAll<HTMLInputElement>("input[type='time']");
+ const duration = () => container.querySelector(".calendar-event-duration")?.textContent;
/** Commits a field as leaving it does — the inputs commit on blur, bound as focusout (see
* the preact/compat note in label_value_input.tsx). */
@@ -52,6 +59,7 @@ describe("EventDatesEditor", () => {
expect(allDayBox()?.checked).toBe(true);
expect(dateInputs()).toHaveLength(2);
expect(timeInputs()).toHaveLength(0);
+ expect(duration()).toBeUndefined();
expect(dateInputs()[0].value).toBe("2026-06-05");
expect(dateInputs()[1].value).toBe("2026-06-07");
@@ -61,6 +69,7 @@ describe("EventDatesEditor", () => {
expect(timeInputs()).toHaveLength(2);
expect(timeInputs()[0].value).toBe("13:00");
expect(timeInputs()[1].value).toBe("14:30");
+ expect(duration()).toBe(" · calendar_view.duration_hours_minutes|1|30");
});
it("switching all-day off gives the day hours to edit and lets a span of days go", () => {
diff --git a/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx b/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx
index 1000ffde14..916efd3139 100644
--- a/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx
+++ b/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx
@@ -8,7 +8,7 @@ import { t } from "../../../services/i18n";
import LabelValueInput from "../../attribute_widgets/label_value_input";
import FormToggle from "../../react/FormToggle";
import { useUniqueName } from "../../react/hooks";
-import { EventField, EventFieldControls, EventFieldRow } from "./EventField";
+import { EventDuration, EventField, EventFieldControls, EventFieldRow } from "./EventField";
import { useEventLabel } from "./hooks";
import { DEFAULT_END_TIME, DEFAULT_START_TIME } from "./selection";
@@ -102,7 +102,10 @@ export default function EventDatesEditor({ note, repeats }: {
</EventField>
{!allDay && (
- <EventField name={t("calendar.dates.time")}>
+ <EventField name={<>
+ {t("calendar.dates.time")}
+ {startDate && <EventDuration event={{ startDate, endDate, startTime, endTime }} />}
+ </>}>
<EventFieldControls className="calendar-event-range">
<LabelValueInput
labelType="time"
diff --git a/apps/client/src/widgets/collections/calendar/EventField.tsx b/apps/client/src/widgets/collections/calendar/EventField.tsx
index f6dd4eec47..88cfaf68e5 100644
--- a/apps/client/src/widgets/collections/calendar/EventField.tsx
+++ b/apps/client/src/widgets/collections/calendar/EventField.tsx
@@ -3,6 +3,9 @@ import "./EventField.css";
import clsx from "clsx";
import { ComponentChildren } from "preact";
+import { t } from "../../../services/i18n";
+import { getTimedDurationMinutes } from "./utils";
+
/**
* The column of an event's own fields — how the event popover says what the calendar draws the
* event by (its recurrence, and in time its start and end dates), those being kept out of the
@@ -19,7 +22,7 @@ export function EventFieldList({ children }: { children: ComponentChildren }) {
* a row of weekday toggles has no single input to point at.
*/
export function EventField({ name, htmlFor, compact, children }: {
- name: string;
+ name: ComponentChildren;
/** The id of the field's input, for a field that has exactly one. */
htmlFor?: string;
/** Take no more of a shared line than the field's own name and control need, leaving the rest
@@ -53,3 +56,22 @@ export function EventFieldControls({ className, children }: {
}) {
return <div className={clsx("calendar-event-field-controls", className)}>{children}</div>;
}
+
+/**
+ * How long a timed event lasts, appended to the line that summarizes when it happens. Renders
+ * nothing for an all-day event. An end before the start reads as running into the next day (see
+ * {@link getTimedDurationMinutes}).
+ */
+export function EventDuration({ event }: { event: Parameters<typeof getTimedDurationMinutes>[0] }) {
+ const total = getTimedDurationMinutes(event);
+ if (total === null) return null;
+
+ const hours = Math.floor(total / 60);
+ const minutes = total % 60;
+ const text = !hours
+ ? t("calendar_view.duration_minutes", { minutes })
+ : !minutes
+ ? t("calendar_view.duration_hours", { hours })
+ : t("calendar_view.duration_hours_minutes", { hours, minutes });
+ return <span className="calendar-event-duration"> · {text}</span>;
+}
diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx b/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx
index 3e2096ab62..1dd642d871 100644
--- a/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx
+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx
@@ -5,6 +5,12 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import GhostPopover from "./GhostPopover";
import { EventDraft } from "./selection";
+// i18next is never initialized under test, so t() renders the key with the interpolated values
+// appended, e.g. "calendar_view.duration_hours_minutes|1|30".
+vi.mock("../../../services/i18n", () => ({
+ t: (key: string, opts?: Record<string, unknown>) => [ key, ...Object.values(opts ?? {}) ].join("|")
+}));
+
describe("GhostPopover", () => {
let container: HTMLElement;
@@ -38,6 +44,7 @@ describe("GhostPopover", () => {
const createButton = () => document.querySelector<HTMLButtonElement>(".calendar-ghost-actions button");
const titleInput = () => document.querySelector<HTMLInputElement>(".calendar-ghost-body input");
const timeInputs = () => [ ...document.querySelectorAll<HTMLInputElement>(".calendar-ghost-body input[type=time]") ];
+ const duration = () => document.querySelector(".calendar-event-duration")?.textContent;
it("asks for the note once, however many times the ask is made before it arrives", async () => {
// A request still out, which is the window each further Enter would make its own note in.
@@ -110,16 +117,19 @@ describe("GhostPopover", () => {
if (!allDay) throw new Error("The ghost has no all-day switch.");
expect(allDay.checked).toBe(true);
expect(timeInputs()).toHaveLength(0);
+ expect(duration()).toBeUndefined();
act(() => { allDay.dispatchEvent(new Event("input", { bubbles: true })); });
const [ start, end ] = timeInputs();
expect(start.value).toBe("09:00");
expect(end.value).toBe("10:00");
+ expect(duration()).toBe(" · calendar_view.duration_hours|1");
start.value = "14:00";
act(() => { start.dispatchEvent(new Event("input", { bubbles: true })); });
end.value = "15:30";
act(() => { end.dispatchEvent(new Event("input", { bubbles: true })); });
+ expect(duration()).toBe(" · calendar_view.duration_hours_minutes|1|30");
await act(async () => { createButton()?.click(); });
expect(onCommit).toHaveBeenCalledWith("", {
diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx b/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
index 93ad5729bc..2972b3777e 100644
--- a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
@@ -10,6 +10,7 @@ import FormTextBox from "../../react/FormTextBox";
import FormToggle from "../../react/FormToggle";
import Modal from "../../react/Modal";
import Popover from "../../react/Popover";
+import { EventDuration } from "./EventField";
import { AnchorPoint, EventDraft, narrowAnchorRect, withAllDay } from "./selection";
/**
@@ -112,7 +113,7 @@ export default function GhostPopover({ draft: initialDraft, anchor, container, o
<div className="calendar-ghost-when">
<span className="bx bx-calendar" />
- <span className="calendar-ghost-dates">{describeDates(draft)}</span>
+ <span className="calendar-ghost-dates">{describeDates(draft)}<EventDuration event={draft} /></span>
<FormToggle
switchOnName={t("calendar.dates.all_day")}
switchOffName={t("calendar.dates.all_day")}
diff --git a/apps/client/src/widgets/collections/calendar/utils.spec.ts b/apps/client/src/widgets/collections/calendar/utils.spec.ts
index 4c08a094d9..27d7ec142b 100644
--- a/apps/client/src/widgets/collections/calendar/utils.spec.ts
+++ b/apps/client/src/widgets/collections/calendar/utils.spec.ts
@@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
import { AttributeRow, BranchRow } from "../../../services/load_results";
import { buildNote } from "../../../test/easy-froca";
-import { formatDateToLocalISO, formatTimeToLocalISO, getMonthsInDateRange, isAttributeChangeAffecting, isBranchChangeAffecting, offsetDate, parseStartEndDateFromEvent, parseStartEndTimeFromEvent } from "./utils";
+import { formatDateToLocalISO, formatTimeToLocalISO, getMonthsInDateRange, getTimedDurationMinutes, isAttributeChangeAffecting, isBranchChangeAffecting, offsetDate, parseStartEndDateFromEvent, parseStartEndTimeFromEvent } from "./utils";
/**
* A moment built in the machine's own zone, which is what the calendar hands these functions: a
@@ -207,3 +207,24 @@ describe("isBranchChangeAffecting", () => {
expect(isBranchChangeAffecting([], "journal", [ "year" ])).toBe(false);
});
});
+
+describe("getTimedDurationMinutes", () => {
+ it("measures a timed event within its day", () => {
+ expect(getTimedDurationMinutes({ startDate: "2026-06-05", startTime: "09:00", endTime: "10:30" })).toBe(90);
+ expect(getTimedDurationMinutes({ startDate: "2026-06-05", startTime: "09:00", endTime: "09:00" })).toBe(0);
+ });
+
+ it("runs an end before the start into the next day, as the calendar draws it", () => {
+ expect(getTimedDurationMinutes({ startDate: "2026-06-05", startTime: "11:00", endTime: "10:00" })).toBe(23 * 60);
+ });
+
+ it("counts the days of a timed event that spans several", () => {
+ expect(getTimedDurationMinutes({ startDate: "2026-03-28", endDate: "2026-03-30", startTime: "22:00", endTime: "01:00" }))
+ .toBe(27 * 60);
+ });
+
+ it("has no duration without both times", () => {
+ expect(getTimedDurationMinutes({ startDate: "2026-06-05" })).toBeNull();
+ expect(getTimedDurationMinutes({ startDate: "2026-06-05", startTime: "09:00" })).toBeNull();
+ });
+});
diff --git a/apps/client/src/widgets/collections/calendar/utils.ts b/apps/client/src/widgets/collections/calendar/utils.ts
index b19347da51..8af64f3af6 100644
--- a/apps/client/src/widgets/collections/calendar/utils.ts
+++ b/apps/client/src/widgets/collections/calendar/utils.ts
@@ -168,6 +168,34 @@ export function isValidDuration(str: string | null | undefined): str is string {
return totalSeconds >= MIN_DURATION_SECONDS && totalSeconds <= MAX_DURATION_SECONDS;
}
+/**
+ * How long a timed event lasts in minutes, in wall-clock time, or `null` for an all-day event or
+ * one missing its end time. An end before the start runs into the next day, as `buildEvent()`
+ * draws it.
+ */
+export function getTimedDurationMinutes({ startDate, endDate, startTime, endTime }: {
+ startDate: string;
+ endDate?: string | null;
+ startTime?: string | null;
+ endTime?: string | null;
+}): number | null {
+ if (!startTime || !endTime) return null;
+
+ const days = endDate && endDate > startDate ? (dayNumber(endDate) - dayNumber(startDate)) : 0;
+ const minutes = days * 24 * 60 + minuteOfDay(endTime) - minuteOfDay(startTime);
+ return minutes < 0 ? minutes + 24 * 60 : minutes;
+}
+
+function dayNumber(isoDate: string) {
+ const [ year, month, day ] = isoDate.split("-").map(Number);
+ return Date.UTC(year, month - 1, day) / (24 * 60 * 60 * 1000);
+}
+
+function minuteOfDay(time: string) {
+ const [ hours, minutes ] = time.split(":").map(Number);
+ return hours * 60 + minutes;
+}
+
// Source: https://stackoverflow.com/a/30465299/4898894
export function getMonthsInDateRange(startDate: string, endDate: string) {
const start = startDate.split("-");
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.html
index 72f53a323d..7b059f4193 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.html
@@ -38,8 +38,9 @@
href="#root/_help_47ZrP6FNuoG8">Default Note Title</a>&nbsp;rules.</p>
<p>To give the event a time before creating it (for example, after clicking
a day in the month view), turn off the <em>All day</em> toggle and set the
- start and end time. A timed event spans a single day, so turning off <em>All day</em> for
- a range of days keeps only the first one.</p>
+ start and end time; the duration is shown next to the date. A timed event
+ spans a single day, so turning off <em>All day</em> for a range of days keeps
+ only the first one.</p>
<p>After creating the event, it will show up on the calendar. To edit the
content of the event (including recurrence), click on it to bring it into
the popup view.</p>
@@ -82,7 +83,8 @@
<li>Calendar-specific features:
<ul>
<li>A toggle for all-day events.</li>
- <li>A start/end date &amp; time selector.</li>
+ <li>A start/end date &amp; time selector. For a timed event, the duration
+ is shown next to the <em>Time</em> label.</li>
<li>A full recurrence editor, to have the same event show up weekly, monthly,
etc.</li>
</ul>
diff --git a/docs/User Guide/User Guide/Collections/Calendar.md b/docs/User Guide/User Guide/Collections/Calendar.md
index 5daceb619d..ed78afdaaa 100644
--- a/docs/User Guide/User Guide/Collections/Calendar.md
+++ b/docs/User Guide/User Guide/Collections/Calendar.md
@@ -28,7 +28,7 @@ In either cases, a small popup will show with a prompt for a title and the date
At this point, the event hasn't been created yet to protect against accidental clicks. Add a title optionally and press the _Create_ button. If no title is provided, the event will follow the <a class="reference-link" href="../Advanced%20Usage/Default%20Note%20Title.md">Default Note Title</a> rules.
-To give the event a time before creating it (for example, after clicking a day in the month view), turn off the _All day_ toggle and set the start and end time. A timed event spans a single day, so turning off _All day_ for a range of days keeps only the first one.
+To give the event a time before creating it (for example, after clicking a day in the month view), turn off the _All day_ toggle and set the start and end time; the duration is shown next to the date. A timed event spans a single day, so turning off _All day_ for a range of days keeps only the first one.
After creating the event, it will show up on the calendar. To edit the content of the event (including recurrence), click on it to bring it into the popup view.
@@ -57,7 +57,7 @@ When an event is clicked, a popup will show near the event which contains the fo
* Button to remove the event from the calendar, which can optionally delete its corresponding note.
* Calendar-specific features:
* A toggle for all-day events.
- * A start/end date & time selector.
+ * A start/end date & time selector. For a timed event, the duration is shown next to the _Time_ label.
* A full recurrence editor, to have the same event show up weekly, monthly, etc.
* The <a class="reference-link" href="../Advanced%20Usage/Attributes/Promoted%20Attributes.md">Promoted Attributes</a> of the marker, if any.
* The note's content which can be edited directly from the panel.