timeAgo static method

String timeAgo(
  1. DateTime dateTime
)

Implementation

static String timeAgo(DateTime dateTime) {
  final now = DateTime.now();
  final difference = now.difference(dateTime);

  if (difference.inMinutes < 1) {
    return 'Just now';
  } else if (difference.inMinutes < 60) {
    return '${difference.inMinutes} min ago';
  } else if (difference.inHours < 24) {
    // return '${difference.inHours} hours ago';
    return DateFormat('h:mm a').format(dateTime); // 12:20 PM
  } else if (difference.inDays == 1) {
    return 'Yesterday';
  } else if (difference.inDays < 7) {
    return DateFormat('EEE').format(dateTime); // Mon, Tue, etc.
  } else if (now.year == dateTime.year) {
    return DateFormat('EEE, d MMM').format(dateTime); // Mon, 12 Jul
  } else {
    return DateFormat('d MMM yyyy').format(dateTime); // 12 Jul 2024
  }
}