Files
cinny/src/app/features/forum/forumTime.ts

41 lines
831 B
TypeScript

/** matrixsso-style relative time (e.g. "4m", "3h", "now"). */
export function formatForumTimeAgo(timestamp: number): string {
const ms = timestamp;
if (!Number.isFinite(ms) || ms <= 0) {
return 'now';
}
const seconds = Math.floor((Date.now() - ms) / 1000);
if (seconds < 45) {
return 'now';
}
const minutes = Math.floor(seconds / 60);
if (minutes < 60) {
return `${minutes}m`;
}
const hours = Math.floor(minutes / 60);
if (hours < 24) {
return `${hours}h`;
}
const days = Math.floor(hours / 24);
if (days < 7) {
return `${days}d`;
}
const weeks = Math.floor(days / 7);
if (weeks < 5) {
return `${weeks}w`;
}
const months = Math.floor(days / 30);
if (months < 12) {
return `${months}mo`;
}
const years = Math.floor(days / 365);
return `${years}y`;
}