The front end calculates the number of days, hours, minutes, and seconds between two time intervals

Following the "Front-end year, month, day, time stamp, China standard time, international standard time conversion", this issue will sort out the calculation of the days, hours, minutes, and seconds of the two time intervals

Front-end year-month-day timestamp China standard time international standard time conversion ( China standard time to date YYYY-MM-DD_M_SSY blog-CSDN blog )

Then calculate the interpolation of the two times? The specific logic is as follows

1. Convert the two times to timestamps, divide the timestamps by 1000, because the timestamps are in milliseconds, we need to get seconds

2. Compare the size of the two timestamps, and use the larger timestamp to subtract the smaller timestamp, which is the difference timestamp

3. To convert the difference, the integer obtained by dividing the difference timestamp by (3600*24) is the number of days, and 3600*24 is the number of seconds in a day

4. Hours, minutes, seconds and so on

The specific code is as follows:

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 + '秒'
    },
},

 

Guess you like

Origin blog.csdn.net/m0_60842861/article/details/131863946