ago method

String ago()

Implementation

String ago() {
  final now = DateTime.now();
  final difference = now.difference(this);

  if (difference.inDays > 365) {
    final years = difference.inDays ~/ 365;
    return Intl.plural(
      years,
      zero: 'No years ago',
      one: '1 year ago',
      other: '$years years ago',
    );
  } else if (difference.inDays > 30) {
    final months = difference.inDays ~/ 30;
    return Intl.plural(
      months,
      zero: 'No months ago',
      one: '1 month ago',
      other: '$months months ago',
    );
  } else if (difference.inDays > 0) {
    return Intl.plural(
      difference.inDays,
      zero: 'Today',
      one: 'Yesterday',
      other: '${difference.inDays} days ago',
    );
  } else if (difference.inHours > 0) {
    return Intl.plural(
      difference.inHours,
      zero: 'Just now',
      one: '1 hour ago',
      other: '${difference.inHours} hours ago',
    );
  } else if (difference.inMinutes > 0) {
    return Intl.plural(
      difference.inMinutes,
      zero: 'Just now',
      one: '1 minute ago',
      other: '${difference.inMinutes} minutes ago',
    );
  } else {
    return 'Just now';
  }
}