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);
}