js怎么模仿微信会话时间戳的显示效果?
今天的显示时分,昨天的显示昨天,前天的显示月日,去年的显示年月日
网友回复
<script> //微信聊天内容时间显示 function timestampFormat(timestamp) { function zeroize(num) { return (String(num).length == 1 ? '0': '') + num; } var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳 var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数 var curDate = new Date(curTimestamp * 1000); // 当前时间日期对象 var tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象 var Y = tmDate.getFullYear(), m = tmDate.getMonth() + 1, d = tmDate.getDate(); var H = tmDate.getHours(), i = tmDate.getMinutes(), s = tmDate.getSeconds(); if (timestampDiff < 60) { // 一分钟以内 return "刚刚"; } else if (timestampDiff < 3600) { // 一小时前之内 return Math.floor(timestampDiff / 60) + "分钟前"; } else if (curDate.getFullYear() == Y && curDate.getMonth()+1 == m && curDate.getDate() == d) { return '今天' + zeroize(H) + ':' + zeroize(i); } else { var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象 if (newDate.getFullYear() == Y && newDate.getMonth()+1 == m && newDate.getDate() == d) { return '昨天' + zeroize(H) + ':' + zeroize(i); } else if (curDate.getFullYear() == Y) { return zeroize(m) + '月' + zeroize(d) + '日 ' + zer...
点击查看剩余70%
ace.js如何获取选择文本的开始和结束行数?
如何把qwen code cli或gemini cli的免费调用额度换成http api对外开放接口?
如何限制windows10电脑只能打开指定的程序?
python如何调用ai大模型实现web网页系统的功能测试并生成测试报告?
有没有免费进行web网站ai仿真人测试生成测试报告的mcp服务或api?
Context Engineering到底是啥,有什么用?
如何使用Google veo 3+高斯溅射(Gaussian Splatting)技术生成4d视频?
浏览器中如何实时调用摄像头扫描二维码?
grok4、gemini2.5pro、gpt5、claude4.1到底谁的编程能力更强一些?
python能将2d平面户型图转换成3d三维户型效果图吗?