Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
1.0 kB · 36 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637export function timeAgo(date: Date) { const seconds = Math.floor((new Date().getTime() - date.getTime()) / 1000); let interval = Math.floor(seconds / 31536000);
// return date.toLocaleDateString("en-US"); if (interval > 1) { const formatter = new Intl.DateTimeFormat("en-US", { year: "numeric", month: "short", day: "numeric", hour: "numeric", minute: "numeric", }); return "on " + formatter.format(date); } interval = Math.floor(seconds / 86400); // return date without years if (interval > 1) { const formatter = new Intl.DateTimeFormat("en-US", { month: "short", day: "numeric", hour: "numeric", minute: "numeric", }); return formatter.format(date); } interval = Math.floor(seconds / 3600); if (interval > 1) { return interval + " hours ago"; } interval = Math.floor(seconds / 60); if (interval > 1) { return interval + " minutes ago"; } return Math.floor(seconds) + " seconds ago";}