getTimeDifference method

String? getTimeDifference({
  1. bool isShowDay = true,
  2. bool isSecond = false,
})

时间戳(Milliseconds)距离当前的时间

Implementation

String? getTimeDifference({bool isShowDay = true, bool isSecond = false}) {
  if (this == null) {
    return null;
  }
  int seconds = this!.toInt();
  if (!isSecond) {
    seconds = seconds ~/ 1000;
  }
  int day = 0;
  int hour = 0;
  int minute = seconds % 3600 ~/ 60;
  int second = seconds % 60;
  if (isShowDay) {
    day = seconds ~/ 3600 ~/ 24;
    hour = seconds ~/ 3600 % 24;
  } else {
    hour = seconds ~/ 3600;
  }
  if (day != 0) {
    return '$day day ${_formatTime(hour)} : ${_formatTime(minute)} : ${_formatTime(second)}';
  } else if (hour != 0) {
    return '${_formatTime(hour)} : ${_formatTime(minute)} : ${_formatTime(second)}';
  } else if (minute != 0) {
    return '${_formatTime(minute)} : ${_formatTime(second)}';
  } else if (second != 0) {
    return '$second s';
  } else {
    return '';
  }
}