Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Keep the picked timeline event date when the browser timezone is not UTC (#85547)

Andersond8a13babd7e9
diff --git a/frontend/src/metabase/forms/components/FormDateInput/FormDateInput.tsx b/frontend/src/metabase/forms/components/FormDateInput/FormDateInput.tsx
index 60d034ca3b2..4721f180b69 100644
--- a/frontend/src/metabase/forms/components/FormDateInput/FormDateInput.tsx
+++ b/frontend/src/metabase/forms/components/FormDateInput/FormDateInput.tsx
@@ -5,6 +5,9 @@ import { forwardRef, useCallback, useMemo } from "react";
import { useUniqueId } from "metabase/common/hooks/use-unique-id";
import { dayjs } from "metabase/dayjs";
import { DateInput, type DateInputProps, type DateValue } from "metabase/ui";
+
+const DATE_FORMAT = "YYYY-MM-DD";
+
export type FormDateInputProps = {
value?: DateValue | undefined;
className?: string;
@@ -39,7 +42,7 @@ export const FormDateInput = forwardRef(function FormDateInput(
const handleChange = useCallback(
(date: DateValue | undefined) => {
if (date) {
- setValue(dayjs(date).format("YYYY-MM-DD"));
+ setValue(dayjs(date).format(DATE_FORMAT));
} else {
setValue(nullable ? null : undefined);
}
@@ -56,7 +59,7 @@ export const FormDateInput = forwardRef(function FormDateInput(
ref={ref}
label={title}
name={name}
- value={dayjs(date).toDate()}
+ value={dayjs(date).format(DATE_FORMAT)}
error={touched && error != null}
onBlur={onBlur}
/>
diff --git a/frontend/src/metabase/forms/components/FormDateInput/FormDateInput.tz.unit.spec.tsx b/frontend/src/metabase/forms/components/FormDateInput/FormDateInput.tz.unit.spec.tsx
new file mode 100644
index 00000000000..001302e93b7
--- /dev/null
+++ b/frontend/src/metabase/forms/components/FormDateInput/FormDateInput.tz.unit.spec.tsx
@@ -0,0 +1,33 @@
+import { render, screen } from "@testing-library/react";
+import { Formik } from "formik";
+
+import { ThemeProvider } from "metabase/ui";
+
+import { FormDateInput } from "./FormDateInput";
+
+const setup = (value: string) => {
+ render(
+ <ThemeProvider>
+ <Formik initialValues={{ date: value }} onSubmit={jest.fn()}>
+ <FormDateInput name="date" title="Date" />
+ </Formik>
+ </ThemeProvider>,
+ );
+};
+
+describe("FormDateInput", () => {
+ it.each([
+ ["2025-12-31T15:00:00Z", "December 31, 2025"],
+ ["2026-01-01T17:00:00Z", "January 1, 2026"],
+ ["2026-01-01", "January 1, 2026"],
+ ])(
+ "should show the date written in %s, not the browser date (#24701)",
+ (value, expectedDate) => {
+ setup(value);
+
+ expect(screen.getByLabelText<HTMLInputElement>("Date").value).toBe(
+ expectedDate,
+ );
+ },
+ );
+});
diff --git a/frontend/src/metabase/timelines/common/components/EventForm/EventForm.tsx b/frontend/src/metabase/timelines/common/components/EventForm/EventForm.tsx
index 3e5aff79819..3f9f87bab5d 100644
--- a/frontend/src/metabase/timelines/common/components/EventForm/EventForm.tsx
+++ b/frontend/src/metabase/timelines/common/components/EventForm/EventForm.tsx
@@ -28,7 +28,6 @@ import {
TimeInput,
} from "metabase/ui";
import * as Errors from "metabase/utils/errors";
-import { parseTimestamp } from "metabase/utils/time-dayjs";
import type {
FormattingSettings,
IconName,
@@ -38,6 +37,12 @@ import type {
import FormArchiveButton from "../FormArchiveButton";
+import {
+ getTimeInputValue,
+ getTimestampWithDate,
+ getTimestampWithTime,
+} from "./utils";
+
const EVENT_SCHEMA = Yup.object({
name: Yup.string().required(Errors.required).max(255, Errors.maxLength),
description: Yup.string().nullable().max(255, Errors.maxLength),
@@ -132,33 +137,33 @@ const EventForm = ({
flex={1}
valueFormat={dateSettings?.date_style}
onChange={(date) => {
- if (values.time_matters) {
- // if time matters, preserve the time part of the timestamp
- // when changing the date part
- const timePart = dayjs.tz(values.timestamp);
- const newDate = parseTimestamp(date)
- .set("hour", timePart.hour())
- .set("minute", timePart.minute());
- setFieldValue("timestamp", newDate.toISOString());
- } else {
- setFieldValue("timestamp", dayjs(date).toISOString());
+ if (date) {
+ setFieldValue(
+ "timestamp",
+ getTimestampWithDate(
+ values.timestamp,
+ date,
+ values.time_matters,
+ ),
+ );
}
}}
/>
{values.time_matters ? (
<Flex gap="xxs" align="end">
<TimeInput
- value={dayjs.tz(values.timestamp).toDate()}
+ value={getTimeInputValue(values.timestamp)}
name="date"
label={t`Time`}
fw="bold"
flex={1}
onChange={(time) => {
- const timePart = dayjs.tz(time);
- const date = parseTimestamp(values.timestamp)
- .set("hour", timePart.hour())
- .set("minute", timePart.minute());
- setFieldValue("timestamp", date.toISOString());
+ if (time) {
+ setFieldValue(
+ "timestamp",
+ getTimestampWithTime(values.timestamp, time),
+ );
+ }
}}
/>
<ActionIcon
diff --git a/frontend/src/metabase/timelines/common/components/EventForm/utils.ts b/frontend/src/metabase/timelines/common/components/EventForm/utils.ts
new file mode 100644
index 00000000000..522d99196dd
--- /dev/null
+++ b/frontend/src/metabase/timelines/common/components/EventForm/utils.ts
@@ -0,0 +1,37 @@
+import { dayjs } from "metabase/dayjs";
+import { parseTimestamp } from "metabase/utils/time-dayjs";
+
+/**
+ * Event timestamps are displayed with the wall clock written in the stored UTC
+ * string, so the form reads and writes that wall clock too, not the browser one.
+ */
+export function getTimestampWithDate(
+ timestamp: string,
+ date: string,
+ timeMatters: boolean,
+): string {
+ const newDate = parseTimestamp(date);
+ if (!timeMatters) {
+ return newDate.toISOString();
+ }
+
+ // if time matters, preserve the time part of the timestamp
+ // when changing the date part
+ const timePart = dayjs.tz(timestamp);
+ return newDate
+ .set("hour", timePart.hour())
+ .set("minute", timePart.minute())
+ .toISOString();
+}
+
+export function getTimestampWithTime(timestamp: string, time: Date): string {
+ const timePart = dayjs.tz(time);
+ return parseTimestamp(timestamp)
+ .set("hour", timePart.hour())
+ .set("minute", timePart.minute())
+ .toISOString();
+}
+
+export function getTimeInputValue(timestamp: string): Date {
+ return dayjs.tz(timestamp).toDate();
+}
diff --git a/frontend/src/metabase/timelines/common/components/EventForm/utils.tz.unit.spec.ts b/frontend/src/metabase/timelines/common/components/EventForm/utils.tz.unit.spec.ts
new file mode 100644
index 00000000000..e04667fb373
--- /dev/null
+++ b/frontend/src/metabase/timelines/common/components/EventForm/utils.tz.unit.spec.ts
@@ -0,0 +1,38 @@
+import {
+ getTimeInputValue,
+ getTimestampWithDate,
+ getTimestampWithTime,
+} from "./utils";
+
+describe("getTimestampWithDate", () => {
+ it("should save the picked date at UTC midnight when the time does not matter (#24701)", () => {
+ expect(
+ getTimestampWithDate("2025-06-15T10:30:00.000Z", "2026-01-01", false),
+ ).toBe("2026-01-01T00:00:00.000Z");
+ });
+
+ it("should keep the time of day when the time matters (#24701)", () => {
+ expect(
+ getTimestampWithDate("2025-06-15T23:30:00.000Z", "2026-01-01", true),
+ ).toBe("2026-01-01T23:30:00.000Z");
+ });
+});
+
+describe("getTimestampWithTime", () => {
+ it("should save the picked time without changing the date (#24701)", () => {
+ expect(
+ getTimestampWithTime(
+ "2026-01-01T00:00:00.000Z",
+ new Date(2025, 5, 15, 23, 30),
+ ),
+ ).toBe("2026-01-01T23:30:00.000Z");
+ });
+});
+
+describe("getTimeInputValue", () => {
+ it("should show the saved time of day (#24701)", () => {
+ const value = getTimeInputValue("2026-01-01T23:30:00Z");
+
+ expect([value.getHours(), value.getMinutes()]).toEqual([23, 30]);
+ });
+});