From 6ae20a8d3272658e4be44f217739ef1f9d73e6d7 Mon Sep 17 00:00:00 2001 From: Djordje Vlaisavljevic Date: Wed, 14 Aug 2024 16:07:11 +0100 Subject: [PATCH] Added relative timestamps to feed objects ref https://linear.app/tryghost/issue/AP-131/show-nice-relative-timestamps --- .../src/utils/get-relative-timestamp.ts | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 apps/admin-x-activitypub/src/utils/get-relative-timestamp.ts diff --git a/apps/admin-x-activitypub/src/utils/get-relative-timestamp.ts b/apps/admin-x-activitypub/src/utils/get-relative-timestamp.ts new file mode 100644 index 0000000000..be70d68c24 --- /dev/null +++ b/apps/admin-x-activitypub/src/utils/get-relative-timestamp.ts @@ -0,0 +1,33 @@ +export const getRelativeTimestamp = (date: Date): string => { + const now = new Date(); + const seconds = Math.floor((now.getTime() - date.getTime()) / 1000); + + let interval = Math.floor(seconds / 31536000); + if (interval > 1) { + return `${interval}y`; + } + + interval = Math.floor(seconds / 2592000); + if (interval > 1) { + return `${interval}m`; + } + + interval = Math.floor(seconds / 86400); + if (interval >= 1) { + return `${interval}d`; + } + + interval = Math.floor(seconds / 3600); + if (interval > 1) { + return `${interval}h`; + } + + interval = Math.floor(seconds / 60); + if (interval > 1) { + return `${interval}m`; + } + + return `${seconds} seconds`; +}; + +export default getRelativeTimestamp; \ No newline at end of file