Added relative timestamps to feed objects

ref https://linear.app/tryghost/issue/AP-131/show-nice-relative-timestamps
This commit is contained in:
Djordje Vlaisavljevic 2024-08-14 16:07:11 +01:00
parent b2d5304c3c
commit 6ae20a8d32

View File

@ -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;