From 661ddfb62d84fbd382766172c5cc36676cb1e40f Mon Sep 17 00:00:00 2001 From: Marc-Aurele Besner Date: Tue, 19 Nov 2024 13:40:15 -0500 Subject: [PATCH] improve precision under 1 minute --- explorer/src/utils/time.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/explorer/src/utils/time.ts b/explorer/src/utils/time.ts index 55b4d298..c3922ad6 100644 --- a/explorer/src/utils/time.ts +++ b/explorer/src/utils/time.ts @@ -35,8 +35,14 @@ export const formatSeconds = (seconds: number | bigint): string => { ) } -export const utcToLocalRelativeTime = (timestamp: string): string => - dayjs.utc(timestamp).local().fromNow(true) + ' ago' +export const utcToLocalRelativeTime = (timestamp: string): string => { + const now = dayjs() + const time = dayjs.utc(timestamp).local() + const diffInSeconds = now.diff(time, 'second') + + if (diffInSeconds < 60) return `${diffInSeconds} seconds ago` + return time.fromNow(true) + ' ago' +} export const utcToLocalTime = (timestamp: string): string => dayjs.utc(timestamp).local().format('DD MMM YYYY | HH:mm:ss(Z)')