extremely wip tangled spa
at main 1.1 kB view raw
1// this was gemini 2export function toRelativeTime(date: Date) { 3 const now = Date.now(); 4 const diffInSeconds = Math.floor((now - date.getTime()) / 1000); 5 6 // Use Math.abs to handle future dates (optional, but good practice) 7 const seconds = Math.abs(diffInSeconds); 8 9 // Define units in descending order. 10 // We added 'week' to match your screenshot style. 11 const units = [ 12 { label: "year", seconds: 31536000 }, 13 { label: "month", seconds: 2592000 }, // ~30 days 14 { label: "week", seconds: 604800 }, // 7 days 15 { label: "day", seconds: 86400 }, 16 { label: "hour", seconds: 3600 }, 17 { label: "minute", seconds: 60 }, 18 { label: "second", seconds: 1 }, 19 ]; 20 21 for (const { label, seconds: unitSeconds } of units) { 22 // We use floor because 13 days is generally considered "1 week ago", not "2 weeks ago" 23 const interval = Math.floor(seconds / unitSeconds); 24 25 if (interval >= 1) { 26 const suffix = interval === 1 ? "" : "s"; 27 const action = diffInSeconds < 0 ? "from now" : "ago"; 28 return `${interval} ${label}${suffix} ${action}`; 29 } 30 } 31 32 return "just now"; 33}