Skip to content

Commit d4eaf85

Browse files
committed
feat: use custom time ago formatter
1 parent 54d75fe commit d4eaf85

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"anchor-js": "^5.0.0",
2828
"astro": "^4.14.3",
2929
"astro-seo": "^0.8.4",
30-
"date-fns": "^3.6.0",
3130
"dompurify": "^3.1.6",
3231
"effect": "^3.6.5",
3332
"fflate": "^0.8.2",

pnpm-lock.yaml

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scripts/time-ago.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1-
import { formatDistanceStrict } from "date-fns";
21
import { defineComponent } from "./define-component";
32

43
export const timeAgo = defineComponent((timestamp: string) => ({
5-
text: formatDistanceStrict(new Date(timestamp), new Date(), { addSuffix: true }),
4+
text: formatTimeAgo(timestamp),
65
}));
6+
7+
const formatTimeAgo = (timestamp: string) => {
8+
const units = [
9+
["year", 365 * 24 * 60 * 60 * 1000],
10+
["month", 30 * 24 * 60 * 60 * 1000],
11+
["day", 24 * 60 * 60 * 1000],
12+
["hour", 60 * 60 * 1000],
13+
["minute", 60 * 1000],
14+
["second", 1000],
15+
] as const;
16+
const diff = Date.now() - new Date(timestamp).getTime();
17+
const elapsed = Math.abs(diff);
18+
for (const [name, size] of units) {
19+
const value = Math.floor(elapsed / size);
20+
if (value > 0) {
21+
const plural = value > 1 ? "s" : "";
22+
const description = `${value} ${name}${plural}`;
23+
return diff > 0 ? `${description} ago` : `in ${description}`;
24+
}
25+
}
26+
return "just now";
27+
};

0 commit comments

Comments
 (0)