From 738a73092dc9b73af682fb15937d441654a338cc Mon Sep 17 00:00:00 2001 From: Anish Pawaskar Date: Fri, 27 Oct 2023 03:21:03 +0530 Subject: [PATCH] fix getIdleSinceText function --- src/helperFunctions/getIdleSinceText.ts | 32 ++++++++++++------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/helperFunctions/getIdleSinceText.ts b/src/helperFunctions/getIdleSinceText.ts index b5689e8a1..2b2a09fd8 100644 --- a/src/helperFunctions/getIdleSinceText.ts +++ b/src/helperFunctions/getIdleSinceText.ts @@ -1,28 +1,26 @@ -import { - TOTAL_MILLISECONDS_IN_A_DAY, - TOTAL_MILLISECONDS_IN_A_HOUR, -} from '@/constants/date'; +import { TOTAL_MILLISECONDS_IN_A_HOUR } from '@/constants/date'; const getIdleSinceText = (idleSince: string) => { const presentDate = new Date(); const idleSinceDate = new Date(idleSince); - const differenceInDay = Math.round( - (presentDate.setUTCHours(0, 0, 0, 0) - - idleSinceDate.setUTCHours(0, 0, 0, 0)) / - TOTAL_MILLISECONDS_IN_A_DAY - ); - const differenceInHours = Math.abs( - Math.round( - (presentDate.getTime() - parseInt(idleSince)) / - TOTAL_MILLISECONDS_IN_A_HOUR - ) + + const presentDateInUtc = presentDate.toUTCString(); + const idleSinceDateInUtc = idleSinceDate.toUTCString(); + + const differenceInHours = Math.round( + (new Date(presentDateInUtc).valueOf() - + new Date(idleSinceDateInUtc).valueOf()) / + TOTAL_MILLISECONDS_IN_A_HOUR ); + const idealDays = Math.round(differenceInHours / 24); - if (differenceInDay > 1) { - return `${differenceInDay} days ago`; - } else { + if (idealDays <= 0) { return `${differenceInHours} hours ago`; + } else if (idealDays === 1) { + return `${idealDays} day ago`; } + + return `${idealDays} days ago`; }; export default getIdleSinceText;