From efc28b00987d8cc63aea56109e221e7a2e78b787 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Tue, 26 Jul 2022 10:03:52 -0500 Subject: Replace momentjs - it is too large of a dependency --- src/view/lib/strings.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/view/lib/strings.ts') diff --git a/src/view/lib/strings.ts b/src/view/lib/strings.ts index 3ef707dd8..30426e674 100644 --- a/src/view/lib/strings.ts +++ b/src/view/lib/strings.ts @@ -21,3 +21,35 @@ export function makeRecordUri( urip.recordKey = recordKey return urip.toString() } + +const MINUTE = 60 +const HOUR = MINUTE * 60 +const DAY = HOUR * 24 +const MONTH = DAY * 30 +const YEAR = DAY * 365 +export function ago(date: number | string | Date): string { + let ts: number + if (typeof date === 'string') { + ts = Number(new Date(date)) + } else if (date instanceof Date) { + ts = Number(date) + } else { + ts = date + } + const diffSeconds = Math.floor((Date.now() - ts) / 1e3) + if (diffSeconds === 0) { + return 'just now' + } else if (diffSeconds < MINUTE) { + return `${diffSeconds}s` + } else if (diffSeconds < HOUR) { + return `${Math.floor(diffSeconds / MINUTE)}m` + } else if (diffSeconds < DAY) { + return `${Math.floor(diffSeconds / HOUR)}h` + } else if (diffSeconds < MONTH) { + return `${Math.floor(diffSeconds / DAY)}d` + } else if (diffSeconds < YEAR) { + return `${Math.floor(diffSeconds / MONTH)}mo` + } else { + return new Date(ts).toLocaleDateString() + } +} -- cgit 1.4.1