From aa9b82b144d625f47a878aaa13c0a648827cae89 Mon Sep 17 00:00:00 2001 From: themashcodee Date: Sat, 2 Nov 2024 19:47:10 +0530 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20fix=20slack=20date=20pars?= =?UTF-8?q?ing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- .../sub_elements/slack_date.tsx | 27 ++++++++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 51bd069..5ae0718 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ ], "repository": "https://github.com/themashcodee/slack-blocks-to-jsx.git", "license": "MIT", - "version": "0.6.1", + "version": "0.6.2", "main": "dist/index.js", "module": "dist/index.mjs", "types": "dist/index.d.ts", diff --git a/src/utils/markdown_parser/sub_elements/slack_date.tsx b/src/utils/markdown_parser/sub_elements/slack_date.tsx index 86719e4..1ef56d2 100644 --- a/src/utils/markdown_parser/sub_elements/slack_date.tsx +++ b/src/utils/markdown_parser/sub_elements/slack_date.tsx @@ -159,7 +159,7 @@ const date_token_to_label = (inputDate: Date, formatString: string): string => { }); } case "{date}": - return dateObj.toLocaleString("en-US", { month: "long", day: "numeric" }); + return dateObj.toLocaleString("en-US", { month: "long", day: "numeric", year: "numeric" }); case "{date_pretty}": { const relativeDay = getRelativeDay(dateObj); if (relativeDay) return relativeDay; @@ -182,18 +182,25 @@ const date_token_to_label = (inputDate: Date, formatString: string): string => { hour12: true, }); case "{ago}": { - const diff = now.getTime() - dateObj.getTime(); - const seconds = Math.floor(diff / 1000); + const diff = dateObj.getTime() - now.getTime(); + const absDiff = Math.abs(diff); + const seconds = Math.floor(absDiff / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); const days = Math.floor(hours / 24); - const years = Math.round(days / 365); - - if (seconds < 60) return `${seconds} second${seconds !== 1 ? "s" : ""} ago`; - if (minutes < 60) return `${minutes} minute${minutes !== 1 ? "s" : ""} ago`; - if (hours < 24) return `${hours} hour${hours !== 1 ? "s" : ""} ago`; - if (days < 365) return `${days} day${days !== 1 ? "s" : ""} ago`; - return `${years} year${years !== 1 ? "s" : ""} ago`; + const months = Math.floor(days / 30); + const years = Math.floor(days / 365); + + const isFuture = diff > 0; + const suffix = isFuture ? "" : " ago"; + const prefix = isFuture ? "in " : ""; + + if (seconds < 60) return `${prefix}${seconds} second${seconds !== 1 ? "s" : ""}${suffix}`; + if (minutes < 60) return `${prefix}${minutes} minute${minutes !== 1 ? "s" : ""}${suffix}`; + if (hours < 24) return `${prefix}${hours} hour${hours !== 1 ? "s" : ""}${suffix}`; + if (days < 30) return `${prefix}${days} day${days !== 1 ? "s" : ""}${suffix}`; + if (months < 12) return `${prefix}${months} month${months !== 1 ? "s" : ""}${suffix}`; + return `${prefix}${years} year${years !== 1 ? "s" : ""}${suffix}`; } default: return formatString;