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