js倒计时,时分秒

243 阅读1分钟
```methods:{
  daojishi(){
        const nowTime = new Date().getTime(); //当前时间
        const startTime = new Date('2022/11/10 20:00:00').getTime() //开始时间
        const endTime = new Date('2022/11/10 22:00:00').getTime()  // 结束时间
        // const day = now.getDate() //日
        if(startTime>nowTime){
           this.isZhibo = false
           this.hour = Math.floor((startTime-nowTime)/(1000*60*60))
           if(Math.floor((startTime-nowTime)/(1000*60*60))<10){
             this.hour = '0'+ Math.floor((startTime-nowTime)/(1000*60*60))
           }
           this.minute = Math.floor((startTime-nowTime)/(1000*60)%60)
            if(Math.floor((startTime-nowTime)/(1000*60)%60)<10){
             this.minute = '0'+ Math.floor((startTime-nowTime)/(1000*60)%60)
           }
           this.seconds = Math.floor((startTime-nowTime)/1000%60)
           if(Math.floor((startTime-nowTime)/1000%60)<10){
             this.seconds = '0'+ Math.floor((startTime-nowTime)/1000%60)
           }
           // console.log(this.hour,this.minute);

        }else if(startTime<nowTime&&nowTime<endTime){
          this.isZhibo = true
        }else if(nowTime>endTime){
          this.isZhibo = 'back'
          return
        }
         // console.log(nowTime,startTime,endTime);
    },
}
created(){
    const nowTime = new Date().getTime(); //当前时间
    const endTime = new Date('2022/11/10 22:00:00').getTime()
    var timer = setInterval(() => {
        this.daojishi()
     },1000)
   if(nowTime>endTime){
       this.isZhibo = 'back'
       clearInterval(timer) //清除定时器
       return
     }
  },