getTimeAgoForChatByMs static method

String getTimeAgoForChatByMs(
  1. int milliSecond, {
  2. String languageCode = 'en',
  3. bool short = false,
})

Implementation

static String getTimeAgoForChatByMs(int milliSecond,
    {String languageCode = 'en', bool short = false}) {
  var nowTime = DateTime.now();
  var dateTime = DateTime.fromMillisecondsSinceEpoch(milliSecond);
  if (nowTime.year != dateTime.year) {
    return DateUtil.formatDate(dateTime,
        format:
            languageCode == "zh" ? "yyyy年MM月dd HH:mm" : "yyyy-MM-dd HH:mm");
  } else if (nowTime.month != dateTime.month) {
    return DateUtil.formatDate(dateTime,
        format: languageCode == "zh" ? "M月dd HH:mm" : "M-dd HH:mm");
  } else if (nowTime.day != dateTime.day) {
    if (isYesterdayByMs(milliSecond, DateTime.now().millisecondsSinceEpoch)) {
      return languageCode == "zh" ? "昨天" : "Yesterday";
    }
    if (isWeek(milliSecond)) {
      var weekday = getWeekdayByMs(milliSecond,
          languageCode: languageCode, short: short);
      return "$weekday ${DateUtil.formatDate(dateTime, format: "HH:mm")}";
    }
    return DateUtil.formatDate(dateTime,
        format: languageCode == "zh" ? "M月dd HH:mm" : "M-dd HH:mm");
  } else {
    final threeMinutesAgo = nowTime.subtract(const Duration(minutes: 3));
    if (dateTime.isAfter(threeMinutesAgo) && dateTime.isBefore(nowTime)) {
      return languageCode == "zh" ? "刚刚" : "just now";
    } else {
      return DateUtil.formatDate(dateTime, format: "HH:mm");
    }
  }
}