formatTimestamp static method

String formatTimestamp(
  1. DateTime timestamp, {
  2. bool showDate = false,
})

Formats a timestamp for display in the chat UI

Implementation

static String formatTimestamp(DateTime timestamp, {bool showDate = false}) {
  final now = DateTime.now();
  final today = DateTime(now.year, now.month, now.day);
  final yesterday = today.subtract(const Duration(days: 1));
  final messageDate = DateTime(timestamp.year, timestamp.month, timestamp.day);

  if (messageDate == today) {
    // Today, just show time
    return DateFormat('h:mm a').format(timestamp);
  } else if (messageDate == yesterday) {
    // Yesterday, show 'Yesterday' with time
    return 'Yesterday, ${DateFormat('h:mm a').format(timestamp)}';
  } else if (now.difference(timestamp).inDays < 7) {
    // Within a week, show day name with time
    return '${DateFormat('EEEE').format(timestamp)}, ${DateFormat('h:mm a').format(timestamp)}';
  } else {
    // Older, show full date with time if requested
    return showDate
        ? DateFormat('MMM d, y, h:mm a').format(timestamp)
        : DateFormat('MMM d, h:mm a').format(timestamp);
  }
}