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。

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

相关推荐
摇光932 分钟前
js高阶-async与事件循环
开发语言·javascript·事件循环·宏任务·微任务
沐泽Mu5 分钟前
嵌入式学习-QT-Day09
开发语言·qt·学习
测试杂货铺9 分钟前
UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·测试用例·pytest
小猿_0011 分钟前
C语言实现顺序表详解
c语言·开发语言
yuanlaile26 分钟前
纯Dart Flutter库适配HarmonyOS
flutter·华为·harmonyos·flutter开发鸿蒙·harmonyos教程
yuanlaile26 分钟前
Flutter开发HarmonyOS 鸿蒙App的好处、能力以及把Flutter项目打包成鸿蒙应用
flutter·华为·harmonyos·flutter开发鸿蒙
余~~1853816280031 分钟前
NFC 碰一碰发视频源码搭建技术详解,支持OEM
开发语言·人工智能·python·音视频
苏三有春42 分钟前
PyQt实战——使用python提取JSON数据(十)
python·json·pyqt
GOATLong43 分钟前
c++智能指针
开发语言·c++