toRelative method
- DateTime? now
Returns relative to now
text representation.
DateTime.now is used if now
is null
.
Implementation
String toRelative([DateTime? now]) {
DateTime local = isUtc ? toLocal() : this;
DateTime relative = now ?? DateTime.now();
int days = relative._julianDayNumber() - local._julianDayNumber();
int months = 0;
if (days >= 28) {
months =
relative.month + relative.year * 12 - local.month - local.year * 12;
if (relative.day < local.day) {
months--;
}
}
if (days > 6) {
return yMd;
}
return 'label_ago_date'.l10nfmt({
'years': months ~/ 12,
'months': months,
'weeks': days ~/ 7,
'days': days,
});
}