forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelativeTime.ts
More file actions
22 lines (21 loc) · 739 Bytes
/
Copy pathrelativeTime.ts
File metadata and controls
22 lines (21 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/** Compact "time ago" label for a past timestamp, relative to `now` (ms). */
export function relativeTime(ts: number, now: number): string {
const s = Math.floor((now - ts) / 1000)
if (s < 5) return 'just now'
if (s < 60) return `${s}s ago`
const m = Math.floor(s / 60)
if (m < 60) return `${m}m ago`
const h = Math.floor(m / 60)
if (h < 24) return `${h}h ago`
return `${Math.floor(h / 24)}d ago`
}
/** True when two epoch-ms timestamps fall on the same local calendar day. */
export function isSameDay(a: number, b: number): boolean {
const da = new Date(a)
const db = new Date(b)
return (
da.getFullYear() === db.getFullYear() &&
da.getMonth() === db.getMonth() &&
da.getDate() === db.getDate()
)
}