vue 实现两个时间戳 相减得到一个倒计时

 

复制代码
<template>

  <div> 距离结束还剩 {{ formattedTime }} </div> </template>

<script>
       
 export default {
    data() {
      return {
         
       endTime: new Date('2029-07-17T00:00:00').getTime(), // 结束时间的时间戳
           currentTime: Date.now(), // 当前时间的时间戳
           countdownInterval: null,
           formattedTime: '',
      };
    },
    computed: {
     
    },
    methods: {
       
       startCountdown() {
              this.countdownInterval = setInterval(() => {
               
                this.currentTime+= 1000;
                const timeRemaining = this.endTime - this.currentTime;
                  if (timeRemaining <= 0) {
                      clearInterval(this.countdownInterval);
                      this.formattedTime = '已结束';
                     
                  } else {
                      this.updateFormattedTime(timeRemaining);
                  }
              }, 1000);
          },
          updateFormattedTime(timeRemaining) {
              

          const seconds = Math.floor((timeRemaining / 1000) % 60);
          const minutes = Math.floor((timeRemaining / 1000 / 60) % 60);
          const hours = Math.floor((timeRemaining / (1000 * 60 * 60)) % 24);
          const days = Math.floor(timeRemaining / (1000 * 60 * 60 * 24));
          this.formattedTime = `${days}天 ${hours}小时 ${minutes}分钟 ${seconds}秒`;

              
          },
    },
    created() {
     
    },
    
   mounted(){
        this.startCountdown();
    },
  };
 
</script>
复制代码

 

posted @   凌晨的粥  阅读(189)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示