前端计算俩个时间间隔天数 时分秒

继《前端 年月日 时间戳 中国标准时间 国际标准时间 转换》之后这期整理一下计算俩个时间间隔的天数 时分秒

前端 年月日 时间戳 中国标准时间 国际标准时间 转换(中国标准时间转日期YYYY-MM-DD_M_SSY的博客-CSDN博客

那么计算俩个时间的插值呢  具体逻辑是这样

1.将俩个时间转为时间戳,时间戳除以1000,因为时间戳是毫秒,我们要拿到秒

2.比较俩个时间戳的大小,利用大的时间戳减去小的时间戳,即为差值时间戳

3.换算差值,差值时间戳除以(3600*24)得到的整数即为天数,3600*24即为一天的秒数

4.小时数  分钟数  秒数依次类推

具体代码如下:

created(){
    console.log(this.time(2023-06-05 12:00:00,2023-06-07 14:00:00))
    //  打印出来的结果为:2天2小时0分0秒
},
methods:{
    time(start,end){
          const startData = Date.parse(start)/1000
          const endData = Date.parse(end)/1000
          let resData = ''
          if(startData > endData){
            resData = startData - endData
          }
          if(startData < endData){
            resData = endData - startData
          }
          if(startData == endData){
            return 0
          }
          var day =  Math.floor(resData / ( 3600 * 24))
          var hour = Math.floor((resData - day*3600*24) / 3600)
          var minute = Math.floor((resData - day*3600*24 - 3600*hour) / 60)
          var second = Math.floor(resData - day*3600*24 - 3600*hour - 60*minute)
          return day + '天' + hour + '小时' + minute +'分'+ second + '秒'
    },
},

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lizy!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值