A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: date rendering to support date formats (#5973)
Shahmir Varqhad6f847ef8cf5
diff --git a/frontend/src/components/data-table/columns.tsx b/frontend/src/components/data-table/columns.tsxindex b16ebfe32f..f53fc113d8 100644--- a/frontend/src/components/data-table/columns.tsx+++ b/frontend/src/components/data-table/columns.tsx@@ -3,10 +3,11 @@import { PopoverClose } from "@radix-ui/react-popover";import type { Column, ColumnDef } from "@tanstack/react-table";+import { formatDate } from "date-fns";import type { DataType } from "@/core/kernel/messages";import type { CalculateTopKRows } from "@/plugins/impl/DataTablePlugin";import { cn } from "@/utils/cn";-import { exactDateTime } from "@/utils/dates";+import { type DateFormat, exactDateTime, getDateFormat } from "@/utils/dates";import { Logger } from "@/utils/Logger";import { Maps } from "@/utils/maps";import { Objects } from "@/utils/objects";@@ -406,12 +407,18 @@ function renderDate(value: Date,dataType?: DataType,dtype?: string,+ format?: DateFormat | null,): React.ReactNode {const type = dataType === "date" ? "date" : "datetime";const timezone = extractTimezone(dtype);++ const exactValue = format+ ? formatDate(value, format)+ : exactDateTime(value, timezone);+return (<DatePopover date={value} type={type}>- {exactDateTime(value, timezone)}+ {exactValue}</DatePopover>);}@@ -432,7 +439,8 @@ export function renderCellValue<TData, TValue>(if (dataType === "datetime" && typeof value === "string") {try {const date = new Date(value);- return renderDate(date, dataType, dtype);+ const format = getDateFormat(value);+ return renderDate(date, dataType, dtype, format);} catch (error) {Logger.error("Error parsing datetime, fallback to string", error);}diff --git a/frontend/src/utils/__tests__/dates.test.ts b/frontend/src/utils/__tests__/dates.test.tsindex f6932b3948..d3c958bb50 100644--- a/frontend/src/utils/__tests__/dates.test.ts+++ b/frontend/src/utils/__tests__/dates.test.ts@@ -1,6 +1,12 @@/* Copyright 2024 Marimo. All rights reserved. */import { afterAll, beforeAll, describe, expect, it } from "vitest";-import { exactDateTime, getShortTimeZone, prettyDate, timeAgo } from "../dates";+import {+ exactDateTime,+ getDateFormat,+ getShortTimeZone,+ prettyDate,+ timeAgo,+} from "../dates";describe("dates", () => {// Save original timezone@@ -161,4 +167,22 @@ describe("dates", () => {expect(getShortTimeZone("MarimoLand")).toBe("MarimoLand");});});++ describe("getDateFormat", () => {+ it("returns the correct format for date", () => {+ expect(getDateFormat("2023-05-15")).toBe("yyyy-MM-dd");+ });++ it("returns the correct format for year only", () => {+ expect(getDateFormat("2023")).toBe("yyyy");+ });++ it("returns the correct format for month only", () => {+ expect(getDateFormat("2023-05")).toBe("yyyy-MM");+ });++ it("returns null for invalid date", () => {+ expect(getDateFormat("2023-05-15T12:00:00Z")).toBeNull();+ });+ });});diff --git a/frontend/src/utils/dates.ts b/frontend/src/utils/dates.tsindex 2215528f99..cef221cf70 100644--- a/frontend/src/utils/dates.ts+++ b/frontend/src/utils/dates.ts@@ -1,7 +1,7 @@/* Copyright 2024 Marimo. All rights reserved. */import { TZDate } from "@date-fns/tz";-import { formatDate } from "date-fns";+import { formatDate, isMatch } from "date-fns";import { Logger } from "./Logger";export function prettyDate(@@ -128,3 +128,18 @@ export function timeAgo(value: string | number | null | undefined): string {return value.toString();}++export const supportedDateFormats = ["yyyy", "yyyy-MM", "yyyy-MM-dd"] as const;+export type DateFormat = (typeof supportedDateFormats)[number];++/**+ * If the string matches one of the supported date formats, return the format.+ */+export function getDateFormat(value: string): DateFormat | null {+ for (const format of supportedDateFormats) {+ if (isMatch(value, format)) {+ return format;+ }+ }+ return null;+}