18 lines
481 B
TypeScript
18 lines
481 B
TypeScript
|
|
/**
|
||
|
|
* Get relative time from timestamp
|
||
|
|
*/
|
||
|
|
export function getRelativeTime(timestamp: number): string {
|
||
|
|
const now = Date.now();
|
||
|
|
const diff = now - timestamp;
|
||
|
|
|
||
|
|
const seconds = Math.floor(diff / 1000);
|
||
|
|
const minutes = Math.floor(seconds / 60);
|
||
|
|
const hours = Math.floor(minutes / 60);
|
||
|
|
const days = Math.floor(hours / 24);
|
||
|
|
|
||
|
|
if (days > 0) return `${days}d ago`;
|
||
|
|
if (hours > 0) return `${hours}h ago`;
|
||
|
|
if (minutes > 0) return `${minutes}m ago`;
|
||
|
|
return 'just now';
|
||
|
|
}
|