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: Support multiple types of timestamps in the Time Component (#4028)

Oleg Isonen34157d4f213c
diff --git a/packages/sdk-components-react/src/time.test.ts b/packages/sdk-components-react/src/time.test.ts
new file mode 100644
index 0000000000..6575c985f8
--- /dev/null
+++ b/packages/sdk-components-react/src/time.test.ts
@@ -0,0 +1,43 @@
+import { expect, test } from "@jest/globals";
+import { parseDate } from "./time";
+
+test("13-digit Unix timestamp", () => {
+ expect(parseDate("1724938577059")).toEqual(
+ new Date("2024-08-29T13:36:17.059Z")
+ );
+});
+
+test("10-digit Unix timestamp ", () => {
+ expect(parseDate("1724938577")).toEqual(new Date("2024-08-29T13:36:17.000Z"));
+});
+
+test("maximum 32-bit signed integer timestamp", () => {
+ expect(parseDate("2147483647")).toEqual(new Date("2038-01-19T03:14:07.000Z"));
+});
+
+test("far future date", () => {
+ expect(parseDate("9999999999999")).toEqual(
+ new Date("2286-11-20T17:46:39.999Z")
+ );
+});
+
+test("parse ISO date string", () => {
+ expect(parseDate("2024-08-29T13:36:17.000Z")).toEqual(
+ new Date("2024-08-29T13:36:17.000Z")
+ );
+});
+
+test("parse short date", () => {
+ expect(parseDate("2024.10")).toEqual(new Date("2024-10-01T00:00:00.000Z"));
+ expect(parseDate("2024/10")).toEqual(new Date("2024-10-01T00:00:00.000Z"));
+ expect(parseDate("2024-10")).toEqual(new Date("2024-10-01T00:00:00.000Z"));
+ expect(parseDate("2024")).toEqual(new Date("2024-01-01T00:00:00.000Z"));
+});
+
+test("empty string", () => {
+ expect(parseDate("")).toEqual(undefined);
+});
+
+test("invalid date string", () => {
+ expect(parseDate("whatever that is")).toEqual(undefined);
+});
diff --git a/packages/sdk-components-react/src/time.tsx b/packages/sdk-components-react/src/time.tsx
index ad821c1a00..8317f9d477 100644
--- a/packages/sdk-components-react/src/time.tsx
+++ b/packages/sdk-components-react/src/time.tsx
@@ -327,6 +327,32 @@ const timeStyleOrUndefined = (
return undefined;
};
+export const parseDate = (datetimeString: string) => {
+ if (datetimeString === "") {
+ return;
+ }
+ let date = new Date(datetimeString);
+
+ // Check if the date already in valid format, e.g. "2024"
+ if (Number.isNaN(date.getTime()) === false) {
+ return date;
+ }
+
+ // If its a number, we assume it's a timestamp and we may need to normalize it
+ if (/^\d+$/.test(datetimeString)) {
+ let timestamp = Number(datetimeString);
+ // Normalize a 10-digit timestamp to 13-digit
+ if (datetimeString.length === 10) {
+ timestamp *= 1000;
+ }
+ date = new Date(timestamp);
+ }
+
+ if (Number.isNaN(date.getTime()) === false) {
+ return date;
+ }
+};
+
export const Time = React.forwardRef<React.ElementRef<"time">, TimeProps>(
(
{
@@ -351,12 +377,10 @@ export const Time = React.forwardRef<React.ElementRef<"time">, TimeProps>(
const datetimeString =
datetime === null ? INVALID_DATE_STRING : datetime.toString();
- const date = new Date(datetimeString);
- const isValidDate = false === Number.isNaN(date.getTime());
-
+ const date = parseDate(datetimeString);
let formattedDate = datetimeString;
- if (isValidDate) {
+ if (date) {
try {
formattedDate = new Intl.DateTimeFormat(locale, options).format(date);
} catch {