timeAgo method
Returns a human-readable string representation of the difference between this date and the current date.
Optional parameters:
abbreviatedUnits
: Iftrue
, displays abbreviated units. Default isfalse
.showRelativeDates
: Iftrue
, displays relative dates for time differences within a day (e.g., "today", "yesterday"). Default isfalse
.precision
: Specifies the number of units to display for the time difference. Default is2
.
Examples:
DateTime dateTime = DateTime(2023, 5, 10, 15, 30);
print(dateTime.diffForHumans()); // Output: "2 days ago"
DateTime pastDateTime = DateTime(2022, 10, 1);
print(pastDateTime.diffForHumans(showRelativeDates: true)); // Output: "7 months ago"
Implementation
String timeAgo({
bool abbreviatedUnits = false,
bool showRelativeDates = false,
int precision = 2,
}) {
final DateTime now = DateTime.now();
final Duration difference = now.difference(this);
final int seconds = difference.inSeconds;
final int minutes = difference.inMinutes;
final int hours = difference.inHours;
final int days = difference.inDays;
if (showRelativeDates && difference.abs().inDays < 2) {
if (difference.isNegative) {
return 'yesterday';
} else if (difference.inDays == 0) {
return 'today';
} else {
return 'tomorrow';
}
}
if (seconds < 5) {
return 'just now';
} else if (seconds < 60) {
return '$seconds${abbreviatedUnits ? 's' : ' seconds'} ago';
} else if (minutes < 60) {
return '$minutes${abbreviatedUnits ? 'm' : ' minutes'} ago';
} else if (hours < 24) {
return '${_formatUnit(hours, abbreviatedUnits, 'hour')} ago';
} else if (days < 30) {
return '${_formatUnit(days, abbreviatedUnits, 'day')} ago';
} else if (days < 365) {
final int months = _calculateMonths(now, this);
return '${_formatUnit(months, abbreviatedUnits, 'month')} ago';
} else {
final int years = days ~/ 365;
return '${_formatUnit(years, abbreviatedUnits, 'year')} ago';
}
}