| /// Formats a [DateTime] as a human-readable relative time string. |
| /// |
| /// Returns short labels like "Just now", "5m ago", "2d ago" for recent dates. |
| /// Falls back to `YYYY-MM-DD` for dates older than 7 days. |
| String formatRelativeDate(DateTime dateTime) { |
| final now = DateTime.now().toUtc(); |
| final difference = now.difference(dateTime.toUtc()); |
| |
| if (difference.inSeconds < 60) { |
| return 'Just now'; |
| } else if (difference.inMinutes < 60) { |
| return '${difference.inMinutes}m ago'; |
| } else if (difference.inHours < 24) { |
| return '${difference.inHours}h ago'; |
| } else if (difference.inDays < 7) { |
| return '${difference.inDays}d ago'; |
| } else { |
| final local = dateTime.toLocal(); |
| return '${local.year}-${local.month.toString().padLeft(2, '0')}-${local.day.toString().padLeft(2, '0')}'; |
| } |
| } |