timeAgo method
Time Ago
Returns string of time difference between given DateTime and
until in the format 1d, 2h, 4s or Just now.
Default values are in English but can be changed by passing the
optional parameters.
remainingis the string to be displayed after the time difference.dayis the string to be displayed after the number of days.houris the string to be displayed after the number of hours.minuteis the string to be displayed after the number of minutes.secondsis the string to be displayed after the number of seconds.justNowis the string to be displayed if the time difference is less than a second.untilis the date to compare with, if not provided, it will use the current date and time.
Implementation
String timeAgo({
string remaining = "remaining",
string day = "d",
string hour = "h",
string minute = "m",
string seconds = "s",
string justNow = "Just now",
date? until,
}) {
final difference = (until ?? now).difference(this);
if (difference.inDays < 0) {
return '${difference.inDays.abs()}$day $remaining';
}
if (difference.inDays >= 1) {
return difference.inDays.quantityText(day);
} else if (difference.inHours >= 1) {
return difference.inHours.quantityText(hour);
} else if (difference.inMinutes >= 1) {
return difference.inMinutes.quantityText(minute);
} else if (difference.inSeconds >= 1) {
return difference.inSeconds.quantityText(seconds);
} else {
return justNow;
}
}