getDateTimeDiffShort static method

dynamic getDateTimeDiffShort(
  1. DateTime previousTime, {
  2. DateTime? currentDate,
})

Implementation

static getDateTimeDiffShort(DateTime previousTime, {DateTime? currentDate}){
  final targetDate = currentDate == null ? DateTime.now() : currentDate;
  final dateDiff = targetDate.difference(previousTime);

  final day = dateDiff.inDays < 0 ? dateDiff.inDays*-1 : dateDiff.inDays;
  if(day > 0){
    return "${day.toInt()}d";
  }

  final hour = dateDiff.inHours < 0 ? dateDiff.inHours*-1 : dateDiff.inHours;
  if(hour > 0){
    return "${hour.toInt()}hr";
  }

  final minute = dateDiff.inMinutes < 0 ? dateDiff.inMinutes*-1 : dateDiff.inMinutes;
  if(minute > 0){
    return "${minute.toInt()}min";
  }

  final second = dateDiff.inSeconds < 0 ? dateDiff.inSeconds*-1 : dateDiff.inSeconds;
  if(second.toInt() < 1){
    return "Now";
  }

  return "${second.toInt()}sec";
}