timeStrByMs static method

String timeStrByMs(
  1. int ms, {
  2. bool showTime = false,
  3. bool showDate = false,
})

Implementation

static String timeStrByMs(
  int ms, {
  bool showTime = false,
  bool showDate = false,
}) {
  // 根据传入时间判断是否是今天、本月、本年,
  // 今天,返回 HH:mm
  // 本月,返回 MM/dd HH:mm
  // 本年,返回 yyyy/MM/dd HH:mm

  TimeType type = _timeType(ms);
  DateTime date = DateTime.fromMillisecondsSinceEpoch(ms);
  String ret = "";
  if (showDate) {
    switch (type) {
      case TimeType.today:
        ret =
            "${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}";
        break;
      case TimeType.month:
        ret =
            "${date.month.toString().padLeft(2, '0')}/${date.day.toString().padLeft(2, '0')} ${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}";
        break;
      case TimeType.year:
        ret =
            "${date.year.toString()}/${date.month.toString().padLeft(2, '0')}/${date.day.toString().padLeft(2, '0')} ${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}";
        break;
    }
  } else {
    ret =
        "${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}";
  }

  return ret;
}