From 0b2c875889bd5392a700ae54c1b9e2770636bd04 Mon Sep 17 00:00:00 2001 From: Dylan De Faoite Date: Thu, 25 Jun 2026 23:17:03 +0100 Subject: [PATCH] fix(datetime): local datetime parsing --- frontend/src/pages/Profile.tsx | 3 ++- frontend/src/utils/utils.ts | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/frontend/src/pages/Profile.tsx b/frontend/src/pages/Profile.tsx index 6203410..1bb946b 100644 --- a/frontend/src/pages/Profile.tsx +++ b/frontend/src/pages/Profile.tsx @@ -3,6 +3,7 @@ import { Eye, EyeOff, Copy, Check } from "lucide-react"; import Box from "../components/Box.tsx"; import { getUser } from "../utils/endpoints.ts"; import type { User } from "../utils/types.ts"; +import { formatLocalDate } from "../utils/utils.ts"; const Profile = () => { const [user, setUser] = useState(null); @@ -44,7 +45,7 @@ const Profile = () => {

@{user.username}

{user.email}

- Member since {new Date(user.createdAt).toLocaleDateString(undefined, { year: "numeric", month: "long", day: "numeric" })} + Member since {formatLocalDate(user.createdAt)}

diff --git a/frontend/src/utils/utils.ts b/frontend/src/utils/utils.ts index e0283c1..7885f8d 100644 --- a/frontend/src/utils/utils.ts +++ b/frontend/src/utils/utils.ts @@ -7,13 +7,20 @@ function formatTime(seconds: number): string { return `${m}:${pad(s)}`; } +// Parses a UTC date string, appending Z if no timezone offset is present so +// the browser always interprets the value as UTC rather than local time. function stringToDate(dateString: string): Date { const normalized = dateString.replace(/(\.\d{3})\d+/, "$1"); - const date = new Date(normalized); + const withTz = /[Zz]$|[+-]\d{2}:\d{2}$|[+-]\d{4}$/.test(normalized) ? normalized : `${normalized}Z`; + const date = new Date(withTz); if (isNaN(date.getTime())) throw new Error("Invalid date string"); return date; } +function formatLocalDate(dateString: string, options?: Intl.DateTimeFormatOptions): string { + return stringToDate(dateString).toLocaleDateString(undefined, options ?? { year: "numeric", month: "long", day: "numeric" }); +} + function dateToTimeAgo(date: Date): string { const secondsAgo = Math.floor((Date.now() - date.getTime()) / 1000); const units: [number, string][] = [ @@ -33,4 +40,4 @@ function dateToTimeAgo(date: Date): string { return `${s} second${s !== 1 ? 's' : ''} ago`; } -export { formatTime, stringToDate, dateToTimeAgo }; +export { formatTime, stringToDate, dateToTimeAgo, formatLocalDate };