formatA static method

String formatA(
  1. int ms, {
  2. int? locMs,
  3. String formatToday = 'HH:mm',
  4. String format = 'yyyy-MM-dd',
  5. String languageCode = 'en',
  6. bool short = false,
})

Timeline like QQ.

today (HH:mm) yesterday (昨天;Yesterday) this week (星期一,周一;Monday,Mon) others (yyyy-MM-dd)

Implementation

static String formatA(
  int ms, {
  int? locMs,
  String formatToday = 'HH:mm',
  String format = 'yyyy-MM-dd',
  String languageCode = 'en',
  bool short = false,
}) {
  int _locTimeMs = locMs ?? DateTime.now().millisecondsSinceEpoch;
  int elapsed = _locTimeMs - ms;
  if (elapsed < 0) {
    return DateUtil.formatDateMs(ms, format: formatToday);
  }

  if (DateUtil.isToday(ms, locMs: _locTimeMs)) {
    return DateUtil.formatDateMs(ms, format: formatToday);
  }

  if (DateUtil.isYesterdayByMs(ms, _locTimeMs)) {
    return languageCode == 'zh' ? '昨天' : 'Yesterday';
  }

  if (DateUtil.isWeek(ms, locMs: _locTimeMs)) {
    return DateUtil.getWeekdayByMs(ms,
        languageCode: languageCode, short: short);
  }

  return DateUtil.formatDateMs(ms, format: format);
}