Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

feat: Add day and month name tokens to Time element format (#5602)

Cosimo Krollc28172eaa2d9
diff --git a/packages/sdk-components-react/src/__generated__/time.props.ts b/packages/sdk-components-react/src/__generated__/time.props.ts
index b37ef21bf0..f210117c1d 100644
--- a/packages/sdk-components-react/src/__generated__/time.props.ts
+++ b/packages/sdk-components-react/src/__generated__/time.props.ts
@@ -217,7 +217,7 @@ export const props: Record<string, PropMeta> = {
},
format: {
description:
- 'Custom format template string. When provided, overrides Date Style and Time Style.\nSupports tokens: YYYY, YY, MM, M, DD, D, HH, H, mm, m, ss, s.\nExample: "YYYY-MM-DD HH:mm:ss" displays as "2025-11-03 18:47:25"',
+ 'Custom format template. Overrides Date Style and Time Style.\n\nTokens: YYYY/YY (year), MMMM/MMM/MM/M (month), DDDD/DDD/DD/D (day), HH/H (hours), mm/m (minutes), ss/s (seconds)\n\nExamples:\n"YYYY-MM-DD" → 2025-11-03\n"DDDD, MMMM D" → Monday, November 3\n"DDD, D. MMM YYYY" → Mon, 3. Nov 2025\n\nDay and month names use the selected language.',
required: false,
control: "text",
type: "string",
diff --git a/packages/sdk-components-react/src/time.test.ts b/packages/sdk-components-react/src/time.test.ts
index 6ccb3b5d02..b3ea34754d 100644
--- a/packages/sdk-components-react/src/time.test.ts
+++ b/packages/sdk-components-react/src/time.test.ts
@@ -78,3 +78,61 @@ test("formatDate with custom separator", () => {
const date = new Date("2025-10-31T14:05:09");
expect(formatDate(date, "DD.MM.YYYY")).toBe("31.10.2025");
});
+
+test("formatDate with full month name in English", () => {
+ const date = new Date("2025-10-31T14:05:09");
+ expect(formatDate(date, "MMMM D, YYYY", "en-US")).toBe("October 31, 2025");
+});
+
+test("formatDate with short month name in English", () => {
+ const date = new Date("2025-10-31T14:05:09");
+ expect(formatDate(date, "MMM D, YYYY", "en-US")).toBe("Oct 31, 2025");
+});
+
+test("formatDate with full day name in English", () => {
+ const date = new Date("2025-10-31T14:05:09");
+ expect(formatDate(date, "DDDD, MMMM D, YYYY", "en-US")).toBe(
+ "Friday, October 31, 2025"
+ );
+});
+
+test("formatDate with short day name in English", () => {
+ const date = new Date("2025-10-31T14:05:09");
+ expect(formatDate(date, "DDD, MMM D", "en-US")).toBe("Fri, Oct 31");
+});
+
+test("formatDate with full month name in German", () => {
+ const date = new Date("2025-10-31T14:05:09");
+ expect(formatDate(date, "D. MMMM YYYY", "de-DE")).toBe("31. Oktober 2025");
+});
+
+test("formatDate with full day name in German", () => {
+ const date = new Date("2025-10-31T14:05:09");
+ expect(formatDate(date, "DDDD, D. MMMM YYYY", "de-DE")).toBe(
+ "Freitag, 31. Oktober 2025"
+ );
+});
+
+test("formatDate with short day and month in Spanish", () => {
+ const date = new Date("2025-10-31T14:05:09");
+ expect(formatDate(date, "DDD, D MMM YYYY", "es-ES")).toBe("vie, 31 oct 2025");
+});
+
+test("formatDate with full day and month in French", () => {
+ const date = new Date("2025-10-31T14:05:09");
+ expect(formatDate(date, "DDDD D MMMM YYYY", "fr-FR")).toBe(
+ "vendredi 31 octobre 2025"
+ );
+});
+
+test("formatDate with mixed tokens", () => {
+ const date = new Date("2025-01-15T09:30:45");
+ expect(formatDate(date, "DDDD, MMMM D, YYYY at HH:mm", "en-US")).toBe(
+ "Wednesday, January 15, 2025 at 09:30"
+ );
+});
+
+test("formatDate preserves non-token text", () => {
+ const date = new Date("2025-10-31T14:05:09");
+ expect(formatDate(date, "Today is DDDD", "en-US")).toBe("Today is Friday");
+});
diff --git a/packages/sdk-components-react/src/time.tsx b/packages/sdk-components-react/src/time.tsx
index 9cd9f8f9e9..77d71f49ec 100644
--- a/packages/sdk-components-react/src/time.tsx
+++ b/packages/sdk-components-react/src/time.tsx
@@ -347,17 +347,36 @@ const parseDate = (datetimeString: string) => {
/**
* Format a date using a template string.
- * Supports tokens: YYYY, YY, MM, M, DD, D, HH, H, mm, m, ss, s
- * Example: formatDate(new Date(), "YYYY-MM-DD HH:mm:ss") => "2025-11-03 18:47:25"
+ * Supports tokens: YYYY, YY, MMMM, MMM, MM, M, DDDD, DDD, DD, D, HH, H, mm, m, ss, s
+ * Example: formatDate(new Date(), "YYYY-MM-DD HH:mm:ss", "en-US") => "2025-11-03 18:47:25"
+ * Example: formatDate(new Date(), "DDDD, MMMM D, YYYY", "en-US") => "Monday, November 3, 2025"
*/
-const formatDate = (date: Date, template: string): string => {
+const formatDate = (date: Date, template: string, locale = "en-US"): string => {
const pad = (n: number, length = 2) => String(n).padStart(length, "0");
+ // Get localized day and month names
+ const longDayName = new Intl.DateTimeFormat(locale, {
+ weekday: "long",
+ }).format(date);
+ const shortDayName = new Intl.DateTimeFormat(locale, {
+ weekday: "short",
+ }).format(date);
+ const longMonthName = new Intl.DateTimeFormat(locale, {
+ month: "long",
+ }).format(date);
+ const shortMonthName = new Intl.DateTimeFormat(locale, {
+ month: "short",
+ }).format(date);
+
const tokens: Record<string, string | number> = {
YYYY: date.getFullYear(),
YY: String(date.getFullYear()).slice(-2),
+ MMMM: longMonthName,
+ MMM: shortMonthName,
MM: pad(date.getMonth() + 1),
M: date.getMonth() + 1,
+ DDDD: longDayName,
+ DDD: shortDayName,
DD: pad(date.getDate()),
D: date.getDate(),
HH: pad(date.getHours()),
@@ -368,9 +387,12 @@ const formatDate = (date: Date, template: string): string => {
s: date.getSeconds(),
};
- return template.replace(/\b(YYYY|YY|MM|M|DD|D|HH|H|mm|m|ss|s)\b/g, (match) =>
- String(tokens[match])
- );
+ // Sort tokens by length (longest first) to avoid partial replacements
+ // e.g., replace MMMM before MMM, DDDD before DDD
+ const sortedTokens = Object.keys(tokens).sort((a, b) => b.length - a.length);
+ const pattern = new RegExp(`\\b(${sortedTokens.join("|")})\\b`, "g");
+
+ return template.replace(pattern, (match) => String(tokens[match]));
};
type TimeProps = Pick<ComponentProps<"time">, "dateTime"> & {
@@ -379,9 +401,16 @@ type TimeProps = Pick<ComponentProps<"time">, "dateTime"> & {
dateStyle?: DateStyle;
timeStyle?: TimeStyle;
/**
- * Custom format template string. When provided, overrides Date Style and Time Style.
- * Supports tokens: YYYY, YY, MM, M, DD, D, HH, H, mm, m, ss, s.
- * Example: "YYYY-MM-DD HH:mm:ss" displays as "2025-11-03 18:47:25"
+ * Custom format template. Overrides Date Style and Time Style.
+ *
+ * Tokens: YYYY/YY (year), MMMM/MMM/MM/M (month), DDDD/DDD/DD/D (day), HH/H (hours), mm/m (minutes), ss/s (seconds)
+ *
+ * Examples:
+ * - "YYYY-MM-DD" → 2025-11-03
+ * - "DDDD, MMMM D" → Monday, November 3
+ * - "DDD, D. MMM YYYY" → Mon, 3. Nov 2025
+ *
+ * Day and month names use the selected language.
*/
format?: string;
};
@@ -419,7 +448,7 @@ export const Time = forwardRef<ElementRef<"time">, TimeProps>(
// Use custom format template if provided
if (format) {
try {
- formattedDate = formatDate(date, format);
+ formattedDate = formatDate(date, format, locale);
} catch {
/* Do Nothing */
}