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。

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

相关推荐
人生游戏牛马NPC1号37 分钟前
学习 Flutter (三):玩安卓项目实战 - 上
android·学习·flutter
橡晟3 小时前
深度学习入门:让神经网络变得“深不可测“⚡(二)
人工智能·python·深度学习·机器学习·计算机视觉
墨尘游子3 小时前
神经网络的层与块
人工智能·python·深度学习·机器学习
倔强青铜33 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian3 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
企鹅与蟒蛇4 小时前
Ubuntu-25.04 Wayland桌面环境安装Anaconda3之后无法启动anaconda-navigator问题解决
linux·运维·python·ubuntu·anaconda
autobaba4 小时前
编写bat文件自动打开chrome浏览器,并通过selenium抓取浏览器操作chrome
chrome·python·selenium·rpa
珊瑚里的鱼4 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上4 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang4 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab