27 lines
700 B
JavaScript
27 lines
700 B
JavaScript
|
|
/**
|
||
|
|
* Human-readable relative time for activity feeds (e.g. "2m ago").
|
||
|
|
*/
|
||
|
|
export function formatRelativeTime(isoString) {
|
||
|
|
if (!isoString) return '';
|
||
|
|
const then = new Date(isoString).getTime();
|
||
|
|
if (Number.isNaN(then)) return '';
|
||
|
|
|
||
|
|
const diffMs = Date.now() - then;
|
||
|
|
if (diffMs < 0) return 'Just now';
|
||
|
|
|
||
|
|
const mins = Math.floor(diffMs / 60000);
|
||
|
|
if (mins < 1) return 'Just now';
|
||
|
|
if (mins < 60) return `${mins}m ago`;
|
||
|
|
|
||
|
|
const hours = Math.floor(mins / 60);
|
||
|
|
if (hours < 24) return `${hours}h ago`;
|
||
|
|
|
||
|
|
const days = Math.floor(hours / 24);
|
||
|
|
if (days < 7) return `${days}d ago`;
|
||
|
|
|
||
|
|
return new Date(isoString).toLocaleDateString(undefined, {
|
||
|
|
month: 'short',
|
||
|
|
day: 'numeric',
|
||
|
|
});
|
||
|
|
}
|