Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix: fix timezone handling in date displays across the app (#2682)

Mohamed Bassem9db69a33f311
diff --git a/apps/web/app/settings/broken-links/page.tsx b/apps/web/app/settings/broken-links/page.tsx
index 82620b0a..6f8f8eae 100644
--- a/apps/web/app/settings/broken-links/page.tsx
+++ b/apps/web/app/settings/broken-links/page.tsx
@@ -5,6 +5,7 @@ import {
SettingsSection,
} from "@/components/settings/SettingsPage";
import { ActionButton } from "@/components/ui/action-button";
+import FormattedDate from "@/components/ui/formatted-date";
import { FullPageSpinner } from "@/components/ui/full-page-spinner";
import { toast } from "@/components/ui/sonner";
import {
@@ -95,8 +96,12 @@ export default function BrokenLinksPage() {
{data.bookmarks.map((b) => (
<TableRow key={b.id}>
<TableCell>{b.url}</TableCell>
- <TableCell>{b.createdAt?.toLocaleString()}</TableCell>
- <TableCell>{b.crawledAt?.toLocaleString()}</TableCell>
+ <TableCell>
+ <FormattedDate date={b.createdAt} />
+ </TableCell>
+ <TableCell>
+ <FormattedDate date={b.crawledAt} />
+ </TableCell>
<TableCell>
{b.isCrawlingFailure ? (
<span className="text-red-500">Failed</span>
diff --git a/apps/web/components/dashboard/highlights/AllHighlights.tsx b/apps/web/components/dashboard/highlights/AllHighlights.tsx
index c7e809ec..f0bfcff6 100644
--- a/apps/web/components/dashboard/highlights/AllHighlights.tsx
+++ b/apps/web/components/dashboard/highlights/AllHighlights.tsx
@@ -21,8 +21,11 @@ import {
import HighlightCard from "./HighlightCard";
function Highlight({ highlight }: { highlight: ZHighlight }) {
- const { fromNow, localCreatedAt } = useRelativeTime(highlight.createdAt);
- const { t } = useTranslation();
+ const { t, i18n } = useTranslation();
+ const { fromNow, localCreatedAt } = useRelativeTime(
+ highlight.createdAt,
+ i18n.language,
+ );
return (
<div className="flex flex-col gap-2">
<HighlightCard highlight={highlight} clickable={false} readOnly={false} />
diff --git a/apps/web/components/dashboard/preview/BookmarkPreview.tsx b/apps/web/components/dashboard/preview/BookmarkPreview.tsx
index 7baffb2e..ba29aad1 100644
--- a/apps/web/components/dashboard/preview/BookmarkPreview.tsx
+++ b/apps/web/components/dashboard/preview/BookmarkPreview.tsx
@@ -57,7 +57,8 @@ function ContentLoading() {
}
function CreationTime({ createdAt }: { createdAt: Date }) {
- const { fromNow, localCreatedAt } = useRelativeTime(createdAt);
+ const { i18n } = useTranslation();
+ const { fromNow, localCreatedAt } = useRelativeTime(createdAt, i18n.language);
return (
<Tooltip delayDuration={0}>
<TooltipTrigger asChild>
@@ -103,7 +104,11 @@ function BookmarkMetadata({ bookmark }: { bookmark: ZBookmark }) {
}
function PublishedDate({ datePublished }: { datePublished: Date }) {
- const { fromNow, localCreatedAt } = useRelativeTime(datePublished);
+ const { i18n } = useTranslation();
+ const { fromNow, localCreatedAt } = useRelativeTime(
+ datePublished,
+ i18n.language,
+ );
return (
<Tooltip delayDuration={0}>
<TooltipTrigger asChild>
diff --git a/apps/web/components/dashboard/search/QueryExplainerTooltip.tsx b/apps/web/components/dashboard/search/QueryExplainerTooltip.tsx
index 96826b1c..73735702 100644
--- a/apps/web/components/dashboard/search/QueryExplainerTooltip.tsx
+++ b/apps/web/components/dashboard/search/QueryExplainerTooltip.tsx
@@ -1,3 +1,4 @@
+import FormattedDate from "@/components/ui/formatted-date";
import InfoTooltip from "@/components/ui/info-tooltip";
import { Table, TableBody, TableCell, TableRow } from "@/components/ui/table";
import {
@@ -59,7 +60,9 @@ export default function QueryExplainerTooltip({
? t("search.not_created_on_or_after")
: t("search.created_on_or_after")}
</TableCell>
- <TableCell>{matcher.dateAfter.toDateString()}</TableCell>
+ <TableCell>
+ <FormattedDate date={matcher.dateAfter} formatStr="PPP" />
+ </TableCell>
</TableRow>
);
case "dateBefore":
@@ -70,7 +73,9 @@ export default function QueryExplainerTooltip({
? t("search.not_created_on_or_before")
: t("search.created_on_or_before")}
</TableCell>
- <TableCell>{matcher.dateBefore.toDateString()}</TableCell>
+ <TableCell>
+ <FormattedDate date={matcher.dateBefore} formatStr="PPP" />
+ </TableCell>
</TableRow>
);
case "age":
diff --git a/apps/web/components/settings/ApiKeySettings.tsx b/apps/web/components/settings/ApiKeySettings.tsx
index 90646b3f..d620592a 100644
--- a/apps/web/components/settings/ApiKeySettings.tsx
+++ b/apps/web/components/settings/ApiKeySettings.tsx
@@ -1,4 +1,5 @@
import { Badge } from "@/components/ui/badge";
+import RelativeTime from "@/components/ui/relative-time";
import {
Table,
TableBody,
@@ -9,7 +10,6 @@ import {
} from "@/components/ui/table";
import { useTranslation } from "@/lib/i18n/server";
import { api } from "@/server/api/client";
-import { formatDistanceToNow } from "date-fns";
import DeleteApiKey from "./DeleteApiKey";
import RegenerateApiKey from "./RegenerateApiKey";
@@ -52,12 +52,14 @@ export default async function ApiKeys({ isAdmin }: { isAdmin: boolean }) {
</div>
</TableCell>
<TableCell>
- {formatDistanceToNow(key.createdAt, { addSuffix: true })}
+ <RelativeTime date={key.createdAt} />
</TableCell>
<TableCell>
- {key.lastUsedAt
- ? formatDistanceToNow(key.lastUsedAt, { addSuffix: true })
- : "—"}
+ {key.lastUsedAt ? (
+ <RelativeTime date={key.lastUsedAt} />
+ ) : (
+ "—"
+ )}
</TableCell>
<TableCell>
<div className="flex items-center gap-2">
diff --git a/apps/web/components/settings/BackupSettings.tsx b/apps/web/components/settings/BackupSettings.tsx
index 525cfc1e..a6bddbed 100644
--- a/apps/web/components/settings/BackupSettings.tsx
+++ b/apps/web/components/settings/BackupSettings.tsx
@@ -3,6 +3,7 @@
import React from "react";
import Link from "next/link";
import { ActionButton } from "@/components/ui/action-button";
+import FormattedDate from "@/components/ui/formatted-date";
import {
Form,
FormControl,
@@ -235,7 +236,9 @@ function BackupRow({ backup }: { backup: z.infer<typeof zBackupSchema> }) {
return (
<TableRow>
- <TableCell>{backup.createdAt.toLocaleString()}</TableCell>
+ <TableCell>
+ <FormattedDate date={backup.createdAt} />
+ </TableCell>
<TableCell>
{backup.status === "pending"
? "-"
diff --git a/apps/web/components/settings/FeedSettings.tsx b/apps/web/components/settings/FeedSettings.tsx
index 9112132a..07870e78 100644
--- a/apps/web/components/settings/FeedSettings.tsx
+++ b/apps/web/components/settings/FeedSettings.tsx
@@ -3,6 +3,7 @@
import React from "react";
import Link from "next/link";
import { ActionButton } from "@/components/ui/action-button";
+import FormattedDate from "@/components/ui/formatted-date";
import {
Form,
FormControl,
@@ -409,7 +410,9 @@ export function FeedRow({ feed }: { feed: ZFeed }) {
>
{feed.url}
</TableCell>
- <TableCell>{feed.lastFetchedAt?.toLocaleString()}</TableCell>
+ <TableCell>
+ <FormattedDate date={feed.lastFetchedAt} />
+ </TableCell>
<TableCell>
{feed.lastFetchedStatus === "success" ? (
<span title="Successful">
diff --git a/apps/web/components/ui/formatted-date.tsx b/apps/web/components/ui/formatted-date.tsx
new file mode 100644
index 00000000..cddfe3de
--- /dev/null
+++ b/apps/web/components/ui/formatted-date.tsx
@@ -0,0 +1,29 @@
+"use client";
+
+import { useEffect, useState } from "react";
+import { formatLocalDate } from "@/lib/date-format";
+import { useTranslation } from "@/lib/i18n/client";
+
+/**
+ * Renders a date formatted on the client side to ensure the user's local
+ * timezone is used. Returns an empty string during SSR so that we never
+ * render a server-timezone date and avoids hydration mismatches.
+ *
+ * The default `formatStr` produces output like "Jan 5, 2025, 3:42 PM".
+ */
+export default function FormattedDate({
+ date,
+ formatStr = "PP, p",
+}: {
+ date: Date | null | undefined;
+ formatStr?: string;
+}) {
+ const { i18n } = useTranslation();
+ const [formatted, setFormatted] = useState("");
+
+ useEffect(() => {
+ setFormatted(date ? formatLocalDate(date, formatStr, i18n.language) : "");
+ }, [date, formatStr, i18n.language]);
+
+ return <>{formatted}</>;
+}
diff --git a/apps/web/components/ui/relative-time.tsx b/apps/web/components/ui/relative-time.tsx
new file mode 100644
index 00000000..332a7c60
--- /dev/null
+++ b/apps/web/components/ui/relative-time.tsx
@@ -0,0 +1,15 @@
+"use client";
+
+import useRelativeTime from "@/lib/hooks/relative-time";
+import { useTranslation } from "@/lib/i18n/client";
+
+export default function RelativeTime({ date }: { date: Date }) {
+ const { i18n } = useTranslation();
+ const { fromNow, localCreatedAt } = useRelativeTime(date, i18n.language);
+
+ return (
+ <time dateTime={date.toISOString()} title={localCreatedAt || undefined}>
+ {fromNow || "—"}
+ </time>
+ );
+}
diff --git a/apps/web/lib/date-format.test.ts b/apps/web/lib/date-format.test.ts
new file mode 100644
index 00000000..5c8b7f61
--- /dev/null
+++ b/apps/web/lib/date-format.test.ts
@@ -0,0 +1,31 @@
+import { describe, expect, it } from "vitest";
+
+import { formatLocalDate, normalizeI18nLanguage } from "./date-format";
+
+describe("normalizeI18nLanguage", () => {
+ it("normalizes app language identifiers for Intl", () => {
+ expect(normalizeI18nLanguage("pt_BR")).toBe("pt-BR");
+ expect(normalizeI18nLanguage("zhtw")).toBe("zh-TW");
+ });
+});
+
+describe("formatLocalDate", () => {
+ const date = new Date("2025-01-05T15:42:00Z");
+
+ it("formats supported locales with localized date and time", () => {
+ expect(formatLocalDate(date, "PP, p", "en")).toBe("Jan 5, 2025, 3:42 PM");
+ expect(formatLocalDate(date, "PPP", "fr")).toBe("5 janvier 2025");
+ });
+
+ it("lets Intl fall back for structurally valid but unsupported locales", () => {
+ expect(() => formatLocalDate(date, "PPP", "zz-ZZ")).not.toThrow();
+ expect(formatLocalDate(date, "PPP", "zz-ZZ")).toBe("January 5, 2025");
+ });
+
+ it("falls back to the runtime locale for malformed locale tags", () => {
+ expect(() => formatLocalDate(date, "PPP", "not_a_locale")).not.toThrow();
+ expect(formatLocalDate(date, "PPP", "not_a_locale")).toBe(
+ "January 5, 2025",
+ );
+ });
+});
diff --git a/apps/web/lib/date-format.ts b/apps/web/lib/date-format.ts
new file mode 100644
index 00000000..ae83c5e7
--- /dev/null
+++ b/apps/web/lib/date-format.ts
@@ -0,0 +1,48 @@
+import { format } from "date-fns";
+
+export function normalizeI18nLanguage(language: string | undefined) {
+ if (!language) {
+ return undefined;
+ }
+
+ if (language === "zhtw") {
+ return "zh-TW";
+ }
+
+ return language.replace("_", "-");
+}
+
+export function formatLocalDate(
+ date: Date,
+ formatStr: string,
+ language?: string,
+) {
+ const locale = normalizeI18nLanguage(language);
+
+ const formatWithIntl = (options: Intl.DateTimeFormatOptions) => {
+ try {
+ return new Intl.DateTimeFormat(locale, options).format(date);
+ } catch (error) {
+ if (error instanceof RangeError) {
+ return new Intl.DateTimeFormat(undefined, options).format(date);
+ }
+
+ throw error;
+ }
+ };
+
+ if (formatStr === "PP, p") {
+ return formatWithIntl({
+ dateStyle: "medium",
+ timeStyle: "short",
+ });
+ }
+
+ if (formatStr === "PPP") {
+ return formatWithIntl({
+ dateStyle: "long",
+ });
+ }
+
+ return format(date, formatStr);
+}
diff --git a/apps/web/lib/hooks/relative-time.ts b/apps/web/lib/hooks/relative-time.ts
index 8fefa233..15e896f4 100644
--- a/apps/web/lib/hooks/relative-time.ts
+++ b/apps/web/lib/hooks/relative-time.ts
@@ -1,7 +1,8 @@
import { useEffect, useState } from "react";
+import { formatLocalDate } from "@/lib/date-format";
import { formatDistanceToNow } from "date-fns";
-export default function useRelativeTime(date: Date) {
+export default function useRelativeTime(date: Date, language?: string) {
const [state, setState] = useState({
fromNow: "",
localCreatedAt: "",
@@ -11,9 +12,9 @@ export default function useRelativeTime(date: Date) {
useEffect(() => {
setState({
fromNow: formatDistanceToNow(date, { addSuffix: true }),
- localCreatedAt: date.toLocaleString(),
+ localCreatedAt: formatLocalDate(date, "PP, p", language),
});
- }, [date]);
+ }, [date, language]);
return state;
}