小程序-将年月日时分秒转为刚刚,几小时前,几天前,几周前,几月前(小程序不兼容Date.parse(),需要将时间中的“-”转为“/”)
=========app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | checkData: function (stringTime){ console.log( "stringTime:" ,stringTime) var minute = 1000 * 60; var hour = minute * 60; var day = hour * 24; var week = day * 7; var month = day * 30; var time1 = new Date().getTime(); //当前的时间戳 console.log( "time1:" ,time1); console.log( "stringTime:" ,stringTime) var time2 = Date.parse(stringTime.replace(/-/g, '/' )); //指定时间的时间戳 console.log( "time2:" ,time2); var time = time1 - time2; console.log( "time:" ,time); var result = null ; if (time < 0) { alert( "设置的时间不能早于当前时间!" ); } else if (time / month >= 1) { result = parseInt(time / month) + "月前" ; } else if (time / week >= 1) { result = parseInt(time / week) + "周前" ; } else if (time / day >= 1) { result = parseInt(time / day) + "天前" ; } else if (time / hour >= 1) { result = parseInt(time / hour) + "小时前" ; } else if (time / minute >= 1) { result = parseInt(time / minute) + "分钟前" ; } else { result = "刚刚发布!" ; } return result; }, |
使用页面js调用方法:app.checkData(‘2022-06-30 11:34:30’)
强调:小程序不兼容Date.parse(),需要将时间中的“-”转为“/”;
Date.parse()通过标准时间格式转换成时间戳,
开发者工具上正常,但是到iphone真机上报错,
原因是iPhone上不支持 2022-06-30 11:34:30 格式,
需转换成 2022/06/30 11:34:30
date = "2022-06-30 11:34:30"
用正则将'-'替换成'/'
let time2 = Date.parse(date.replace(/-/g, '/'))
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具