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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具