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.
remaining
is the string to be displayed after the time difference.day
is the string to be displayed after the number of days.hour
is the string to be displayed after the number of hours.minute
is the string to be displayed after the number of minutes.seconds
is the string to be displayed after the number of seconds.justNow
is the string to be displayed if the time difference is less than a second.until
is 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;
}
}