From a70316e64bcc9bad7fb626056f9cc58d1858dff9 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 21 Jan 2025 23:26:32 -0800 Subject: notes --- src/lib/utils.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/lib') diff --git a/src/lib/utils.ts b/src/lib/utils.ts index e2e19b05..2e1bb840 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -4,6 +4,12 @@ import { page } from "$app/stores"; import { PUBLIC_COINOS_NETWORK, PUBLIC_COINOS_URL } from "$env/static/public"; import { bytesToHex } from "@noble/hashes/utils"; import { toast } from "@zerodevx/svelte-toast"; +import { + differenceInDays, + differenceInHours, + differenceInMinutes, + differenceInSeconds, +} from "date-fns"; import { decrypt } from "nostr-tools/nip49"; import { get as getStore } from "svelte/store"; @@ -434,3 +440,19 @@ export const versions = { }[PUBLIC_COINOS_NETWORK]; export const str = (s) => s.toString(); + +export const ago = (t) => { + const now = new Date(); + const date = new Date(t * 1000); + const seconds = differenceInSeconds(now, date); + if (seconds < 60) return `${seconds}s`; + + const minutes = differenceInMinutes(now, date); + if (minutes < 60) return `${minutes}m`; + + const hours = differenceInHours(now, date); + if (hours < 24) return `${hours}h`; + + const days = differenceInDays(now, date); + return `${days}d`; +}; -- cgit v1.2.3