/ src / lib / utils / date.ts
date.ts
 1  /**
 2   * Formats a date string as a compact relative time (e.g., "1h", "8d"),
 3   * or "Month 'YY" if older than 2 months.
 4   */
 5  function formatShortDate(dateString: string | null): string {
 6      if (!dateString) return 'Never';
 7      
 8      const date = new Date(dateString);
 9      const now = new Date();
10      const diffMs = now.getTime() - date.getTime();
11      
12      const diffMins = Math.floor(diffMs / (1000 * 60));
13      const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
14      const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
15      const diffMonths = diffMs / (1000 * 60 * 60 * 24 * 30.44);
16  
17      // If older than 2 months, use "Month YYYY"
18      if (diffMonths > 2) {
19          const month = new Intl.DateTimeFormat('en-US', { month: 'short' }).format(date);
20          const year = new Intl.DateTimeFormat('en-US', { year: 'numeric' }).format(date);
21          return `${month} ${year}`;
22      }
23  
24      // Compact relative formatting
25      if (diffMins < 1) return 'now';
26      if (diffMins < 60) return `${diffMins}m`;
27      if (diffHours < 24) return `${diffHours}h`;
28      if (diffDays < 30) return `${diffDays}d`;
29      
30      // Fallback for between 1 and 2 months if needed
31      return `${Math.floor(diffMonths)}mo`;
32  }
33  
34  /** Legacy alias for compatibility */
35  export const formatLastStatus = formatShortDate;