樱花树下'

导航

vue中的倒计时实现方法

js部分

      // 获取现在时间
      gettime(){
        let nowtime = new Date()
        let nowmiao = nowtime.getTime()//现在的秒数

        let settime = new Date("2060-11-30 13:22:20")
        let setmiao = settime.getTime()//要设置的秒数
  

        let toolmiao =setmiao-nowmiao//秒数差

        if (toolmiao>=0) { //赋值 
              this.myday = Math.floor(toolmiao/1000/60/60/24); 

              this.myh = Math.floor(toolmiao/1000/60/60%24); 
              //补0操作 
              if(this.myh<10){
                this.myh = "0"+this.myh
              }
              this.mym = Math.floor(toolmiao/1000/60%60); 
              //补0操作 
              if(this.mym<10){
                this.mym = "0"+this.mym
              } 
              //补0操作 
              this.mys = Math.floor(toolmiao/1000%60); 
              if(this.mys<10){
                this.mys = "0"+this.mys
              }                     
        } 
      },

  

1.settime需要一个标准格式的时间

2.在mounted中
mounted() {
let timer = setInterval(() => {
    this.gettime()
}, 1000);


},
 



posted on 2021-11-24 09:58  樱花树下'  阅读(533)  评论(0编辑  收藏  举报