首页 > 其他分享 >js实现用户评论时间与当前时间对比,显示"n分钟前","n小时前",以及具体日期的效果

js实现用户评论时间与当前时间对比,显示"n分钟前","n小时前",以及具体日期的效果

时间:2023-01-06 19:33:30浏览次数:51  
标签:12 moment js 日期 let 时间 createTime

这里需要用到moment.js插件

官网:Moment.js 中文网 (momentjs.cn)

我这里将插件的实例挂载到了this.$moment上

 

                let initTime = e.createTime // 格式 2022-12-12 12:30:51
                // let a = this.$moment().diff(this.$moment('2023/1/5 20:15:00'))/1000;
                let a = this.$moment().diff(this.$moment(e.createTime))/1000;
                // 只换算到小时
                const T = ['分钟', '小时']
                const t = [60, 60]
                console.log('换算前', a)
                
                for (var i = 0; i < t.length; i++) {
                    if (parseInt(a/t[i])) {
                        a = parseInt(a/t[i])
                        e.createTime = a + T[i] + '前'
                        // 创建日期与当前日期进行对比,相差小于一天则显示n分钟前,n小时前,大于一天直接显示日期
                        if(T[i] = "小时" && a > 24) {
                            e.createTime = initTime.split(' ')[0]
                        }
                    }
                }
                console.log('换算后', e.createTime)

 

标签:12,moment,js,日期,let,时间,createTime
From: https://www.cnblogs.com/p201821460026/p/17031441.html

相关文章

  • 前端浅谈 - js的垃圾回收
    1.对于js来说什么是垃圾?    垃圾就是没用了的东西.emmm~~对于js来说,这种说法不是特别准确但是又特别贴切.占着内存但是又不被需要的变量被称为垃圾(有被内涵到).......
  • hdu:Choose the best route(dijstra,虚设点)
    ProblemDescriptionOneday,Kikiwantstovisitoneofherfriends.Assheisliabletocarsickness,shewantstoarriveatherfriend’shomeassoonasp......
  • Nodejs Mysql 执行多条SQL语句
    NodejsMysql执行多条SQL语句最近用Nodejs+Express+Mysql写接口碰到一个需要四表联查的接口。。。我特么只会连两张表啊~!网上百度了一波四表联查,由于我接口的数据......
  • Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/datatype/jsr310
    Causedby:java.lang.NoClassDefFoundError:com/fasterxml/jackson/datatype/jsr310/ser/ZoneIdSerializer 1、报错信息SpringBoot项目启动的时候,报错如下:Caused......
  • js set get hook
    普通方法的hook非常soeasy,就不用记录了。现在记录一下hooksetget方法:classA{constructor(){this._age=0;}getage(){returnthis._age;......
  • 本人博客html + css + js 模板
    简介送给想设计主页的朋友一些思路 HTML<!DOCTYPEhtml><html><body><style>#nav{width:150px;height:400px;border:1pxsolid#D4CD49;position:fixe......
  • 用js控制flash来实现全屏效果
    说起来真是惭愧,学习flash都三四年了,flash在网页中全屏的问题一直都没有得到比较好的解决。今天在看js方面的书籍,flash始终是镶嵌于网页中的插件,与浏览器有着相依为命的关系......
  • 解析curl的 json 响应 并赋值
    youcanusepython,whichexistsinalllinuxbasedsystems,toparsethejsonforyouhttps://askubuntu.com/questions/714458/bash-script-store-curl-output-......
  • js原生xhr请求XMLHttpRequest
    创建一个请求实例,发送请求varxhr=newXMLHttpRequest();xhr.open('GET','test.php');xhr.send();监控XMLHttpRequest对象的状态变化xhr.onreadystatechange=......
  • js content-type
    letxhr=newXMLHttpRequest()xhr.open('POST','http://xx.aa.cn/home/moban/ssds11')xhr.setRequestHeader('Content-Type','application/json')xhr.send(......