Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

time.ts

20 lines720 Bytes

1export function relativeTime(input: string): string {
2 const timestamp = Date.parse(input);
3 if (Number.isNaN(timestamp)) {
4 return "<1m";
5 }
6​
7 // Anything under a minute renders as "<1m" rather than a live seconds count.
8 // The seconds ticker changed width every second and reflowed the surrounding row.
9 const deltaSeconds = Math.max(0, Math.floor((Date.now() - timestamp) / 1000));
10 if (deltaSeconds < 60) return "<1m";
11​
12 const deltaMinutes = Math.floor(deltaSeconds / 60);
13 if (deltaMinutes < 60) return `${deltaMinutes}m`;
14​
15 const deltaHours = Math.floor(deltaMinutes / 60);
16 if (deltaHours < 24) return `${deltaHours}h`;
17​
18 const deltaDays = Math.floor(deltaHours / 24);
19 return `${deltaDays}d`;