timeAgo static method

String timeAgo(
  1. String? date, {
  2. bool numericDates = false,
})

Implementation

static String timeAgo(String? date, {bool numericDates = false}) {
  DateTime dateTime = DateTime.parse(date!).toLocal();
  Duration diff = DateTime.now().difference(dateTime);
  if (diff.inMinutes < 60) {
    if(diff.inMinutes == 0){
      return 'Just now';
    }
    return numericDates
        ? '${diff.inMinutes} ${diff.inMinutes > 1 ? 'mins': 'min'}'
        : DateFormat.Hm().format(dateTime);
  }
  if (diff.inHours < 24)
    return numericDates
        ? '${diff.inHours} ${diff.inHours > 1 ? 'hrs': 'hr'}'
        : DateFormat.Hm().format(dateTime);
  if (diff.inDays < 2)
    return numericDates ? '1 day' : 'Yesterday';
  if (diff.inDays < 3)
    return numericDates ? '2 days' : '2 days ago';
  if (diff.inDays < 4)
    return numericDates ? '3 days' : '3 days ago';
  if (diff.inDays < 365) return DateFormat('d MMM').format(dateTime);
  return DateFormat.yMMMd().format(dateTime);
}