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。

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

相关推荐
深科文库24 分钟前
构建 MCP 服务器:第 4 部分 — 创建工具
python·chatgpt·prompt·aigc·agi·ai-native
witton29 分钟前
美化显示LLDB调试的数据结构
数据结构·python·lldb·美化·debugger·mupdf·pretty printer
sunly_1 小时前
Flutter:导航固定背景图,滚动时导航颜色渐变
android·javascript·flutter
SteveDraw1 小时前
C++动态链接库封装,供C#/C++ 等编程语言使用——C++动态链接库概述(总)
开发语言·c++·c#·封装·动态链接库
十五年专注C++开发1 小时前
设计模式之单例模式(二): 心得体会
开发语言·c++·单例模式·设计模式
nenchoumi31191 小时前
AirSim/Cosys-AirSim 游戏开发(一)XBox 手柄 Windows + python 连接与读取
windows·python·xbox
GoodStudyAndDayDayUp1 小时前
初入 python Django 框架总结
数据库·python·django
星辰大海的精灵1 小时前
基于Dify+MCP实现通过微信发送天气信息给好友
人工智能·后端·python
精灵vector2 小时前
Agent短期记忆的几种持久化存储方式
人工智能·python
flyair_China2 小时前
【云架构】
开发语言·php