Your music, beautifully tracked. All yours. (coming soon) teal.fm
teal-fm atproto

Add timeAgo util to format elapsed time

Formats time difference as seconds, minutes, hours, or date string
for longer intervals.

Natalie B 8f1bedb4 354ccce6

Changed files
+37
apps
amethyst
lib
+37
apps/amethyst/lib/utils.ts
··· 10 10 let first = arr.shift()?.toUpperCase(); 11 11 return (first || "") + arr.join(""); 12 12 } 13 + 14 + export function timeAgo(date: Date) { 15 + const seconds = Math.floor((new Date().getTime() - date.getTime()) / 1000); 16 + let interval = Math.floor(seconds / 31536000); 17 + 18 + // return date.toLocaleDateString("en-US"); 19 + if (interval > 1) { 20 + const formatter = new Intl.DateTimeFormat("en-US", { 21 + year: "numeric", 22 + month: "short", 23 + day: "numeric", 24 + hour: "numeric", 25 + minute: "numeric", 26 + }); 27 + return "on " + formatter.format(date); 28 + } 29 + interval = Math.floor(seconds / 86400); 30 + // return date without years 31 + if (interval > 1) { 32 + const formatter = new Intl.DateTimeFormat("en-US", { 33 + month: "short", 34 + day: "numeric", 35 + hour: "numeric", 36 + minute: "numeric", 37 + }); 38 + return "on " + formatter.format(date); 39 + } 40 + interval = Math.floor(seconds / 3600); 41 + if (interval > 1) { 42 + return interval + " hours ago"; 43 + } 44 + interval = Math.floor(seconds / 60); 45 + if (interval > 1) { 46 + return interval + " minutes ago"; 47 + } 48 + return Math.floor(seconds) + " seconds ago"; 49 + }