From 56ceb68e0f999c8515afa6fa582b52c0e4203932 Mon Sep 17 00:00:00 2001 From: Leander Furumo Date: Wed, 27 Nov 2024 16:13:30 +0100 Subject: [PATCH] use built in relative time formatter --- src/app/components/EventCard.tsx | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/app/components/EventCard.tsx b/src/app/components/EventCard.tsx index a0ba8b5..7f5ca0b 100644 --- a/src/app/components/EventCard.tsx +++ b/src/app/components/EventCard.tsx @@ -219,15 +219,20 @@ const getTimeDifference = (timeDiff: number): string => { const msInHour = 1000 * 60 * 60; const msInDay = msInHour * 24; + const rtf = new Intl.RelativeTimeFormat("en-US", { + numeric: "always", // "auto" will show "tomorrow" if day = 1 + style: "long", + }); + if (timeDiff < msInHour) { const minutesLeft = Math.floor(timeDiff / (1000 * 60)); - return `in ${minutesLeft} minutes`; + return rtf.format(minutesLeft, "minute") } else if (timeDiff < msInDay) { const hoursLeft = Math.floor(timeDiff / msInHour); - return `in ${hoursLeft} hours`; + return rtf.format(hoursLeft, "hour") } else { const daysLeft = Math.floor(timeDiff / msInDay); - return `in ${daysLeft} days`; + return rtf.format(daysLeft, "day") } };