效果
时间动态变化
js部分
<script setup>
import { onMounted, ref, onBeforeUnmount } from 'vue';
let nowtime = ref();
const getTime = () => {
let date1 = new Date();
let year = date1.getFullYear();
let month = date1.getMonth();
let day = date1.getDate();
let xinqi = date1.getDay();
let w = ['日', '一', '二', '三', '四', '五', '六'];
let hh = date1.getHours();
let mm = date1.getMinutes();
let ss = date1.getSeconds() < 10 ? '0' + date1.getSeconds() : date1.getSeconds();
//let date = year + "年" + (month + 1) + "月" + day + "日 星期" + xinqi + " " + hh + ":" + mm + ":" + ss
let date = year + '年' + (month + 1) + '月' + day + '日';
//let week = "星期" +xinqi //星期6
let week = '星期' + w[xinqi]; //星期六
let realTime = hh + ':' + mm + ':' + ss;
document.getElementById('tobaccoTime1').innerHTML = date;
document.getElementById('tobaccoTime2').innerHTML = week;
document.getElementById('tobaccoTime3').innerHTML = realTime;
// console.log(date, week, realTime); //最终得到时间 2022年11月24日 星期四 16:10:45
};
onMounted(() => {
//启动定时器
nowtime.value = setInterval(() => {
getTime();
}, 1000);
});
onBeforeUnmount(() => {
// 在组件消费前,销毁定时器
clearInterval(nowtime.value);
});
</script>
html部分
<template>
<div class="header">
<div class="timer">
<div id="headerTime" class="header-time text">
<span class="header-time-span1" id="tobaccoTime1"></span>
<span class="header-time-span2 middle" id="tobaccoTime2"></span>
<span class="header-time-span3 middle" id="tobaccoTime3"></span>
</div>
</div>
<div class="title">*****平台</div>
</div>
</template>