summarizeDate function

String summarizeDate(
  1. String loadedDate
)

Summarizes a given date/time string into a human-readable format.

This function takes a loaded date/time string as input and summarizes it into a human-readable format, such as "X mins ago", "Y hours ago", or "Yesterday". It calculates the time difference between the input date and the current date, and generates appropriate summaries based on the time elapsed. If the date parsing fails, the current date is used as a fallback. The function also uses the formatDate function to provide a detailed date format when needed.

@param loadedDate The date/time string to summarize. @return A human-readable summarized format of the input date/time.

Implementation

String summarizeDate(String loadedDate) {
  final DateTime parsedDate =
      DateTime.tryParse(loadedDate)?.toLocal() ?? DateTime.now();
  final int difference = DateTime.now().difference(parsedDate).inMinutes;

  if (difference < 1) {
    return '0 mins ago';
  } else if (difference == 1) {
    return '1 min ago';
  } else if (difference < 60) {
    return '$difference mins ago';
  } else if (difference < 1440) {
    return '${(difference / 60).round()} hours ago';
  } else if (difference < 2880) {
    return 'Yesterday';
  }
  return formatDate(loadedDate);
}