From dba6dd19836501e595da5d7efe91bf78134de532 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Mon, 9 Jun 2025 16:49:09 +0530 Subject: [PATCH] fix: future date is not captured in pretty date --- frontend/src/utils/index.js | 54 ++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/frontend/src/utils/index.js b/frontend/src/utils/index.js index 68a2abe2..f2633b2f 100644 --- a/frontend/src/utils/index.js +++ b/frontend/src/utils/index.js @@ -86,11 +86,32 @@ export function prettyDate(date, mini = false) { let dayDiff = Math.floor(diff / 86400) - if (isNaN(dayDiff) || dayDiff < 0) return '' + if (isNaN(dayDiff)) return '' if (mini) { // Return short format of time difference - if (dayDiff == 0) { + if (dayDiff < 0) { + if (Math.abs(dayDiff) < 1) { + if (diff < 60) { + return __('now') + } else if (diff < 3600) { + return __('in {0} m', [Math.floor(diff / 60)]) + } else if (diff < 86400) { + return __('in {0} h', [Math.floor(diff / 3600)]) + } + } + if (Math.abs(dayDiff) == 1) { + return __('tomorrow') + } else if (Math.abs(dayDiff) < 7) { + return __('in {0} d', [Math.abs(dayDiff)]) + } else if (Math.abs(dayDiff) < 31) { + return __('in {0} w', [Math.floor(Math.abs(dayDiff) / 7)]) + } else if (Math.abs(dayDiff) < 365) { + return __('in {0} M', [Math.floor(Math.abs(dayDiff) / 30)]) + } else { + return __('in {0} y', [Math.floor(Math.abs(dayDiff) / 365)]) + } + } else if (dayDiff == 0) { if (diff < 60) { return __('now') } else if (diff < 3600) { @@ -111,7 +132,34 @@ export function prettyDate(date, mini = false) { } } else { // Return long format of time difference - if (dayDiff == 0) { + if (dayDiff < 0) { + if (Math.abs(dayDiff) < 1) { + if (diff < 60) { + return __('just now') + } else if (diff < 120) { + return __('in 1 minute') + } else if (diff < 3600) { + return __('in {0} minutes', [Math.floor(diff / 60)]) + } else if (diff < 7200) { + return __('in 1 hour') + } else if (diff < 86400) { + return __('in {0} hours', [Math.floor(diff / 3600)]) + } + } + if (Math.abs(dayDiff) == 1) { + return __('tomorrow') + } else if (Math.abs(dayDiff) < 7) { + return __('in {0} days', [Math.abs(dayDiff)]) + } else if (Math.abs(dayDiff) < 31) { + return __('in {0} weeks', [Math.floor(Math.abs(dayDiff) / 7)]) + } else if (Math.abs(dayDiff) < 365) { + return __('in {0} months', [Math.floor(Math.abs(dayDiff) / 30)]) + } else if (Math.abs(dayDiff) < 730) { + return __('in 1 year') + } else { + return __('in {0} years', [Math.floor(Math.abs(dayDiff) / 365)]) + } + } else if (dayDiff == 0) { if (diff < 60) { return __('just now') } else if (diff < 120) {