forked from jimrandomh/faceclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate-util.ts
More file actions
31 lines (28 loc) · 1.03 KB
/
Copy pathdate-util.ts
File metadata and controls
31 lines (28 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
export function formatTimestamp(timestampMs: number): string {
const date = new Date(timestampMs);
return `${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`;
}
export function formatAgeShortFromTimestamp(timestampMs: number | null, nowMs: number): string {
if (!timestampMs) {
return "--";
}
const elapsedMinutes = Math.max(0, Math.round((nowMs - timestampMs) / 60_000));
if (elapsedMinutes < 60) {
return `${elapsedMinutes}m`;
}
const elapsedHours = Math.round(elapsedMinutes / 60);
if (elapsedHours < 48) {
return `${elapsedHours}h`;
}
return `${Math.round(elapsedHours / 24)}d`;
}
export function formatRelativeTime(timestamp: number): string {
if (!timestamp) return "";
const ageMs = Math.max(0, Date.now() - timestamp);
const minutes = Math.floor(ageMs / 60_000);
if (minutes < 1) return "now";
if (minutes < 60) return `${minutes}m`;
const hours = Math.floor(minutes / 60);
if (hours < 24) return `${hours}h`;
return `${Math.floor(hours / 24)}d`;
}