timeAgo method

String timeAgo(
  1. BuildContext context, {
  2. bool useAbbreviation = false,
})

Implementation

String timeAgo(BuildContext context, {bool useAbbreviation = false}) {
  final _currentTime = DateTime.now();
  final _time = this;

  if (_time == null) return 'null';

  if (_currentTime.day == _time.day && _currentTime.year == _time.year) {
    return timeFormat(context);
  } else if (_time.isBefore(_currentTime) && _currentTime.difference(_time).inHours < 24) {
    return useAbbreviation ? _abbreviate('Yesterday') : 'Yesterday';
  } else if (_time.isBefore(_currentTime) && _currentTime.difference(_time).inDays < 7) {
    final _day = DateFormat('EEEE').format(_time);
    return useAbbreviation ? _abbreviate(_day) : _day;
  } else {
    return DateFormat('dd/MM/yy').format(_time);
  }
}