Live video on the AT Protocol
at eli/optional-convergence 36 lines 1.0 kB view raw
1export function timeAgo(date: Date) { 2 const seconds = Math.floor((new Date().getTime() - date.getTime()) / 1000); 3 let interval = Math.floor(seconds / 31536000); 4 5 // return date.toLocaleDateString("en-US"); 6 if (interval > 1) { 7 const formatter = new Intl.DateTimeFormat("en-US", { 8 year: "numeric", 9 month: "short", 10 day: "numeric", 11 hour: "numeric", 12 minute: "numeric", 13 }); 14 return "on " + formatter.format(date); 15 } 16 interval = Math.floor(seconds / 86400); 17 // return date without years 18 if (interval > 1) { 19 const formatter = new Intl.DateTimeFormat("en-US", { 20 month: "short", 21 day: "numeric", 22 hour: "numeric", 23 minute: "numeric", 24 }); 25 return formatter.format(date); 26 } 27 interval = Math.floor(seconds / 3600); 28 if (interval > 1) { 29 return interval + " hours ago"; 30 } 31 interval = Math.floor(seconds / 60); 32 if (interval > 1) { 33 return interval + " minutes ago"; 34 } 35 return Math.floor(seconds) + " seconds ago"; 36}