cDateToAging method

String cDateToAging()

Converts the DateTime instance into a human-readable aging string.

This method calculates the difference between the current DateTime instance and the current date and time and provides an aging description.

Example:

final pastDate = DateTime(2022, 3, 10);
final agingString = pastDate.cDateToAging();
print(agingString); // Output: "1 year ago"

Implementation

String cDateToAging() {
  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: '1 day ago',
      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';
  }
}