getDateTimeDiffInShort static method

String getDateTimeDiffInShort(
  1. DateTime previousTime, {
  2. DateTime? currentDate,
})

Implementation

static String getDateTimeDiffInShort(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 > 31){
//      if(day > 365){
      return dateToString(previousTime, format: FORMAT_d_MMM_yyyy);
//      }
//      return dateToString(previousTime, format: FORMAT_d_MMM);
  }

  if(day > 0){
    return "${day.toInt()}d";
  }

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

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

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