From 417c1799ddccc04461c494e93893eb3ec97aea6c Mon Sep 17 00:00:00 2001 From: Achintya Chatterjee <55826451+Achintya-Chatterjee@users.noreply.github.com> Date: Fri, 12 Jul 2024 02:00:03 +0530 Subject: [PATCH] Fix : Showing wrong ETA for tasks (#602) * fix: wrong eta showing on task * fix:wrong eta --- app/helpers/convertDate.js | 56 ++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/app/helpers/convertDate.js b/app/helpers/convertDate.js index ca744d60..e7e235c9 100644 --- a/app/helpers/convertDate.js +++ b/app/helpers/convertDate.js @@ -7,57 +7,47 @@ const timeDifference = (timestamp, timeNow) => { const mins = calc(60, timeInSec); const hours = calc(60, mins); const days = calc(24, hours); - const weeks = calc(7, days); - const months = calc(30, days); - const years = calc(12, months); - let result = years; - let cycle = 'year'; + const months = Math.floor(days / 30); + const years = Math.floor(months / 12); + + console.log({ timeInSec, mins, hours, days, months, years }); // Debug log if (timeInSec < 1) { - return 'just now'; + return { result: '', cycle: 'just now' }; } if (years > 0) { - result = years; - cycle = 'year'; + return { result: years, cycle: 'year' }; } else if (months > 0) { - result = months; - cycle = 'month'; - } else if (weeks > 0) { - result = weeks; - cycle = 'week'; + return { result: months, cycle: 'month' }; } else if (days > 0) { - result = days; - cycle = 'day'; + return { result: days, cycle: 'day' }; } else if (hours > 0) { - result = hours; - cycle = 'hour'; + return { result: hours, cycle: 'hour' }; } else if (mins > 0) { - result = mins; - cycle = 'minute'; + return { result: mins, cycle: 'minute' }; } else { - result = ''; - cycle = 'few seconds'; + return { result: '', cycle: 'few seconds' }; } - return { result, cycle }; }; function convertDate([timestamp], { end_date, timeNow = Date.now() }) { if (!timestamp) return 'TBD'; - if (end_date == 1 && timestamp * 1000 < timeNow) { - const time_value = timeDifference(timestamp, timeNow); - return `${time_value.result} ${time_value.cycle}${ - time_value.result > 1 ? 's' : '' - } ago`; - } const time_value = timeDifference(timestamp, timeNow); - if (timestamp * 1000 < timeNow) - return `${time_value.result} ${time_value.cycle}${ - time_value.result > 1 ? 's ago' : ' ago' - }`; - return `in ${time_value.result} ${time_value.cycle}${ + + let timeString = `${time_value.result} ${time_value.cycle}${ time_value.result > 1 ? 's' : '' }`; + + if (end_date == 1 && timestamp * 1000 < timeNow) { + return `${timeString} ago`; + } + + if (timestamp * 1000 < timeNow) { + return `${timeString} ago`; + } + + return `in ${timeString}`; } export default helper(convertDate);