flutter开发实战-时间显示刚刚几分钟前几小时前

flutter开发实战-时间显示刚刚几分钟前几小时前

在开发中经常遇到从服务端获取的时间戳,需要转换显示刚刚、几分钟前、几小时前、几天前、年月日等格式。

一、代码实现

dart 复制代码
static String timeFormatterChatTimeStamp(int seconds) {
    try {
      int nowDateSeconds =
          (DateTime.now().millisecondsSinceEpoch / 1000).truncate();

      String anotherDate =
          DateUtil.formatDateMs(seconds*1000, format: DateFormats.full);

      String nowDate =
          DateUtil.formatDateMs(nowDateSeconds*1000, format: DateFormats.full);

      print(
          "timeFormatterChatTimeStamp "
              "seconds:${seconds},"
              "nowDateSeconds:${nowDateSeconds},"
              "anotherDate:${anotherDate},"
              "nowDate:${nowDate}");
      //为了判断当前时间是否为未来时间
      if (seconds > nowDateSeconds) {
        return anotherDate;
      }

      List<String> anotherDateList = anotherDate.split(" ");
      List<String> nowDateList = nowDate.split(" ");
      if (anotherDateList.length == 2 && nowDateList.length == 2) {
        String nowDateYMD = nowDateList[0];
        String nowDateHMS = nowDateList[1];

        String anotherDateYMD = anotherDateList[0];
        String anotherDateHMS = anotherDateList[1];

        List<String> anotherDateYMDList = anotherDateYMD.split("-");
        List<String> nowDateYMDList = nowDateYMD.split("-");

        List<String> anotherDateHMSList = anotherDateHMS.split(":");
        List<String> nowDateHMSList = nowDateHMS.split(":");

        String anotherDateY = anotherDateYMDList[0];
        String anotherDateM = anotherDateYMDList[1];
        String anotherDateD = anotherDateYMDList[2];

        String nowDateY = nowDateYMDList[0];
        String nowDateM = nowDateYMDList[1];
        String nowDateD = nowDateYMDList[2];

        String anotherDateH = anotherDateHMSList[0];
        String anotherDateMi = anotherDateHMSList[1];
        String anotherDateS = anotherDateHMSList[2];

        int year = int.parse(anotherDateY) - int.parse(nowDateY);
        if (year < 0) {
          // 过去(今年以前)
          return "${anotherDateY}年${anotherDateM}月${anotherDateD}日 ${anotherDateH}:${anotherDateMi}";
        } else {
          if (nowDateY == anotherDateY) {
            // 今年
            return "${anotherDateM}月${anotherDateD}日 ${anotherDateH}:${anotherDateMi}";
          } else {
            return "${anotherDateY}年${anotherDateM}月${anotherDateD}日 ${anotherDateH}:${anotherDateMi}";
          }
        }
      } else {
        return anotherDate;
      }
    } catch (e) {
      return "${seconds}";
    }
  }

二、小结

flutter开发实战-格式化时间显示刚刚几分钟前几小时前等

从服务端获取的时间戳,需要转换显示刚刚、几分钟前、几小时前、几天前、年月日等格式。用到了DateUtil。

学习记录,每天不停进步。

相关推荐
小陈phd8 分钟前
李宏毅机器学习笔记——梯度下降法
人工智能·python·机器学习
kk爱闹16 分钟前
【挑战14天学完python和pytorch】- day01
android·pytorch·python
Blossom.11827 分钟前
机器学习在智能建筑中的应用:能源管理与环境优化
人工智能·python·深度学习·神经网络·机器学习·机器人·sklearn
亚力山大抵37 分钟前
实验六-使用PyMySQL数据存储的Flask登录系统-实验七-集成Flask-SocketIO的实时通信系统
后端·python·flask
showyoui1 小时前
Python 闭包(Closure)实战总结
开发语言·python
今天背单词了吗9801 小时前
算法学习笔记:7.Dijkstra 算法——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·开发语言·数据结构·笔记·算法
Eiceblue1 小时前
使用 C# 发送电子邮件(支持普通文本、HTML 和附件)
开发语言·c#·html·visual studio
小小小小王王王1 小时前
hello判断
开发语言·c#
amazinging1 小时前
北京-4年功能测试2年空窗-报培训班学测开-第四十一天
python·学习·appium
amazinging2 小时前
北京-4年功能测试2年空窗-报培训班学测开-第三十九天
python·学习·appium