Skip to content

Commit

Permalink
fix: 🐛 fix slack date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
themashcodee committed Nov 2, 2024
1 parent ca65f0f commit aa9b82b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 17 additions & 10 deletions src/utils/markdown_parser/sub_elements/slack_date.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit aa9b82b

Please sign in to comment.