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

前端JS处理时间,适用于聊天、操作记录等(包含刚刚、x分钟前、x小时前、x天前)

export default {// 首页时间转化formatDate(val) {var nowDate = new Date()var oldDate = new Date(val)const Y = oldDate.getFullYear()const M = oldDate.getMonth() + 1const D = oldDate.getDate()var diff = nowDate.getTime() - oldDate.getTime()var minutes = Math.floor(diff / (1000 * 60))var hours = Math.floor(diff / (1000 * 60 * 60))var days = Math.floor(diff / (1000 * 60 * 60 * 24))// return hours + ':' + minutesif (minutes < 1) {return '刚刚'} else if (minutes < 60) {return minutes + '分钟前'} else if (hours < 24) {return hours + '小时前'} else {if (days < 7) {return days + '天前'} else {return Y + '/' + M + '/' + D}}},// 聊天时,发送时间处理formatTime(value) {var nowDate = new Date()var oldDate = new Date(value)const Y = oldDate.getFullYear()const M = oldDate.getMonth() + 1const D = oldDate.getDate()const h = oldDate.getHours() > 9 ? oldDate.getHours() : '0' + oldDate.getHours()const m = oldDate.getMinutes() > 9 ? oldDate.getMinutes() : '0' + oldDate.getMinutes()const s = oldDate.getSeconds() > 9 ? oldDate.getSeconds() : '0' + oldDate.getSeconds()var diff = nowDate.getTime() - oldDate.getTime()var minutes = Math.floor(diff / (1000 * 60))var hours = Math.floor(diff / (1000 * 60 * 60))var days = Math.floor(diff / (1000 * 60 * 60 * 24))// return hours + ':' + minutesif (minutes < 1) {// return '刚刚'} else if (minutes < 60) {return minutes + '分钟前'} else if (hours < 24) {return hours + '小时前'} else {if (days < 3) {return days + '天前 ' + h + ':' + m + ':' + s} else {return Y + '年' + M + '月' + D + '日 ' + ' ' + h + ':' + m + ':' + s}}}}

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

相关文章:

  • Windows已经安装了一个MySQL8,通过修改配置文件的端口号跑2个或多个Mysql服务方法,并注册为系统服务
  • lesson36:MySQL从入门到精通:全面掌握数据库操作与核心原理
  • 嵌入式系统学习Day17(文件编程)
  • 项目实战2——LAMP_LNMP实践
  • 智能化评估体系:数据生产、在线化与自动化的三重奏
  • 解锁 Appium Inspector:移动端 UI 自动化定位的利器
  • 【论文阅读】一种基于经典机器学习的肌电下肢意图检测方法,用于人机交互系统
  • Secure CRT做代理转发
  • 【element树组件】el-tree实现连接线及hover编辑效果
  • ip归属地批量查询脚本
  • 视频输入输出模块介绍和示例
  • 【Node.js从 0 到 1:入门实战与项目驱动】2.1 安装 Node.js 与 npm(Windows/macOS/Linux 系统的安装步骤)
  • history命令增强记录执行时间与登录IP
  • 线性代数 · 矩阵 | 最小多项式
  • 【debug 解决 记录】stm32 debug模式的时候可以运行,但是烧录没法执行
  • Mac如何安装telnet命令
  • 论答题pk小程序软件版权的
  • 家政小程序系统开发:推动家政行业数字化转型,共创美好未来
  • 校园快递小程序(腾讯地图API、二维码识别、Echarts图形化分析)
  • 基于开源AI大模型AI智能名片S2B2C商城小程序的母婴用品精准营销策略研究
  • 思科、华为、华三如何切换三层端口?
  • Web前端小游戏轮盘。
  • 盲盒抽谷机小程序系统开发:解锁盲盒新玩法,开启潮玩社交新时代
  • OpenHarmony介绍
  • 计算机网络---默认网关(Default Gateway)
  • 飞算JavaAI全流程实操指南:从需求到部署的智能开发体验
  • 本地(macOS)和服务器时间不同步导致的 Bug排查及解决
  • web应用服务器——Tomcat
  • Linux 常用命令大全:覆盖日常 99% 操作需求
  • A4.1:继C5.3引申的基极偏置的应用和发射极偏置的负反馈电阻讨论