From 843c1b0895a4f6b4c19451bad47f4880d29e3dee Mon Sep 17 00:00:00 2001 From: Pas <74743263+Pasithea0@users.noreply.github.com> Date: Sun, 3 Aug 2025 16:46:58 -0600 Subject: [PATCH] add days ago counter --- .../notificationsModal/utils/index.ts | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/components/overlays/notificationsModal/utils/index.ts b/src/components/overlays/notificationsModal/utils/index.ts index 8641a027..86080f03 100644 --- a/src/components/overlays/notificationsModal/utils/index.ts +++ b/src/components/overlays/notificationsModal/utils/index.ts @@ -52,16 +52,52 @@ export const getSourceName = (feedUrl: string): string => { } }; +const getRelativeTimeString = (date: Date): string => { + const now = new Date(); + const diffInMs = now.getTime() - date.getTime(); + const diffInDays = Math.floor(diffInMs / (1000 * 60 * 60 * 24)); + + if (diffInDays === 0) { + const diffInHours = Math.floor(diffInMs / (1000 * 60 * 60)); + if (diffInHours === 0) { + const diffInMinutes = Math.floor(diffInMs / (1000 * 60)); + if (diffInMinutes === 0) { + return "just now"; + } + return `${diffInMinutes} minute${diffInMinutes !== 1 ? "s" : ""} ago`; + } + return `${diffInHours} hour${diffInHours !== 1 ? "s" : ""} ago`; + } + if (diffInDays === 1) { + return "1 day ago"; + } + if (diffInDays < 7) { + return `${diffInDays} days ago`; + } + if (diffInDays < 30) { + const weeks = Math.floor(diffInDays / 7); + return `${weeks} week${weeks !== 1 ? "s" : ""} ago`; + } + if (diffInDays < 365) { + const months = Math.floor(diffInDays / 30); + return `${months} month${months !== 1 ? "s" : ""} ago`; + } + const years = Math.floor(diffInDays / 365); + return `${years} year${years !== 1 ? "s" : ""} ago`; +}; + export const formatDate = (dateString: string) => { try { const date = new Date(dateString); - return date.toLocaleDateString("en-US", { + const formattedDate = date.toLocaleDateString("en-US", { year: "numeric", month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", }); + const relativeTime = getRelativeTimeString(date); + return `${formattedDate} • ${relativeTime}`; } catch { return dateString; }