当前位置: 首页 > news >正文

uniapp 格式化时间刚刚,几分钟前,几小时前,几天前…

效果如图:

根目录下新建utils文件夹,文件夹下新增js文件,文件内容:

export const filters = {dateTimeSub(data) {if (data == undefined) {return;}// 传进来的data必须是日期格式,不能是时间戳//将字符串转换成时间格式var timePublish = new Date(data);var timeNow = new Date();var minute = 1000 * 60;var hour = minute * 60;var day = hour * 24;var month = day * 30;var result = "";var diffValue = timeNow - timePublish;  //时间差var diffMonth = diffValue / month;      //月差var diffWeek = diffValue / (7 * day);   //周差var diffDay = diffValue / day;          //天差var diffHour = diffValue / hour;        //时差var diffMinute = diffValue / minute;    //分差if (diffValue < 0) {console.log("错误时间");} else if (diffMonth > 3) {// result = timePublish.getFullYear() + "-";// result += timePublish.getMonth() + "-";// result += timePublish.getDate();// console.log(result);} else if (diffMonth > 1) {// result = parseInt(diffMonth) + "月前";} else if (diffWeek > 1) {// result = parseInt(diffWeek) + "周前";} else if (diffDay > 3) {let y = timePublish.getFullYear();let MM = timePublish.getMonth() + 1;MM = MM < 10 ? ('0' + MM) : MM; //月补0let d = timePublish.getDate();d = d < 10 ? ('0' + d) : d; //天补0result = y + '-' + MM + '-' + d;} else if (diffDay>=1 && diffDay <= 3) {result = parseInt(diffDay) + "天前";} else if (diffHour > 1) {result = parseInt(diffHour) + "小时前";} else if (diffMinute > 1) {result = parseInt(diffMinute) + "分钟前";} else {result = "刚刚";}return result;},formatDate(value) {if (value == undefined) {return;}// #ifdef APP-PLUSif(plus.os.name == 'iOS') {time = time.replaceAll("-", "/")}// #endif// let date = new Date(value * 1000);let date = new Date(value);//时间戳为10位需*1000,时间戳为13位的话不需乘1000let y = date.getFullYear();let MM = date.getMonth() + 1;MM = MM < 10 ? ('0' + MM) : MM; //月补0let d = date.getDate();d = d < 10 ? ('0' + d) : d; //天补0let h = date.getHours();h = h < 10 ? ('0' + h) : h; //小时补0let m = date.getMinutes();m = m < 10 ? ('0' + m) : m; //分钟补0let s = date.getSeconds();s = s < 10 ? ('0' + s) : s; //秒补0return y + '-' + MM + '-' + d; //年月日// return y + '-' + MM + '-' + d + ' ' + h + ':' + m+ ':' + s; //年月日时分秒},formatTime(value) {if (value == undefined) {return;}// #ifdef APP-PLUSif(plus.os.name == 'iOS') {time = time.replaceAll("-", "/")}// #endiflet date = new Date(value);let MM = date.getMonth() + 1;MM = MM < 10 ? ('0' + MM) : MM; //月补0let d = date.getDate();d = d < 10 ? ('0' + d) : d; //天补0return  MM + '月' + d + '日'; //年月日}
}

 main.js中引入

import Vue from 'vue'Vue.config.productionTip = falseimport { filters } from './utils/time_day.js'
// 定义全局自定义过滤器
Object.keys(filters).forEach(key => {Vue.filter(key, filters[key])
})

页面中使用即可

<view class="times">{{item.updatetime | getDateDiff}}</view>

http://www.lryc.cn/news/123012.html

相关文章:

  • JProfiler —CPU评测
  • 994. 腐烂的橘子
  • Rx.NET in Action 第三章学习笔记
  • Windows11环境下VS2019调用Pytorch语义分割模型(C++版)
  • Milkv Duo 以太网使用与配置
  • bash: make: command not found
  • 热点如何用于期刊写作——以chatGPT为例
  • IGV.js 的完全本地化运行探索
  • 网络安全渗透测试之靶场训练
  • Java课题笔记~ Spring 的事务管理
  • 仿到位|独立版家政上门预约服务小程序家政保洁师傅上门服务小程序上门服务在线派单源码
  • Observability:识别生成式 AI 搜索体验中的慢速查询
  • 接口测试及接口抓包常用的测试工具
  • CH342/CH343/CH344/CH347/CH9101/CH9102/CH9103/CH9104 Linux串口驱动使用教程
  • 反射和工厂设计模式---工厂设计模式
  • 【算法——双指针】LeetCode 283 移动零
  • 腾讯云轻量服务器和云服务器的CPU处理器有差别吗?
  • Redis_亿级访问量数据处理
  • Java-类型和变量(基于C语言的补充)
  • 机器学习笔记:李宏毅diffusion model
  • STM32--TIM定时器(2)
  • git Authentication failed
  • 【软考】2023系统架构设计师考试
  • opencv基础55-获取轮廓的特征值及示例
  • OpenCV图像处理——形态学操作
  • 修改VS Code终端的显示行数
  • C++学习| MFC简单入门
  • “一日之际在于晨”,欢迎莅临WAVE SUMMIT上午场:Arm 虚拟硬件早餐交流会
  • leetcode454. 四数相加 II
  • PHP证券交易员学习网站mysql数据库web结构apache计算机软件工程网页wamp