cDateToAging method

String cDateToAging()

Converts a date string into a human-readable aging format.

Returns a human-readable aging description of the date string.

Example:

String date = '2023-08-01T12:34:56';
String agingDescription = date.cDateToAging();
// Result: '1 month ago'

Implementation

String cDateToAging() {
  try {
    final now = DateTime.now();
    final difference = now.difference(DateTime.parse(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';
    }
  } catch (e) {
    return this;
  }
}