Hey is a decentralized and permissionless social media app built with Lens Protocol 馃尶
at main 34 lines 834 B view raw
1import dayjs from "dayjs"; 2 3const formatRelativeOrAbsolute = (date: Date | string) => { 4 const now = dayjs(); 5 const targetDate = dayjs(date); 6 const diffInDays = now.diff(targetDate, "day"); 7 const diffInHours = now.diff(targetDate, "hour"); 8 const diffInMinutes = now.diff(targetDate, "minute"); 9 const diffInSeconds = now.diff(targetDate, "second"); 10 11 if (diffInDays >= 1) { 12 // More than a day 13 return diffInDays < 7 14 ? `${diffInDays}d` 15 : targetDate.format( 16 now.isSame(targetDate, "year") ? "MMM D" : "MMM D, YYYY" 17 ); 18 } 19 20 if (diffInHours >= 1) { 21 // More than an hour 22 return `${diffInHours}h`; 23 } 24 25 if (diffInMinutes >= 1) { 26 // More than a minute 27 return `${diffInMinutes}m`; 28 } 29 30 // Seconds 31 return `${diffInSeconds}s`; 32}; 33 34export default formatRelativeOrAbsolute;