
<template>
<div>
<div>
<p>开始时间:{{ state.stm }}</p>
<p>结束时间:{{ state.etm }}</p>
<p>相差(数字格式):{{state.ztime.timeList.join(':')}}</p>
<p>相差(中文格式):{{state.ztime.timeName}}</p>
</div>
</div>
</template>
<script setup>
import { onMounted, reactive } from 'vue';
import dayjs from 'dayjs';
const getMinuteToFormat = (e) =>{
let tname = ''
let h = Math.floor(e / (60 * 60))
let m = Math.floor((e - (h * 60 *60)) / 60)
let s = Math.floor(e % 60)
h = h>9 ?h :'0'+h
m = m>9 ?m :'0'+m
s = s>9 ?s :'0'+s
if(h>0){ tname += h + '时' }
if(m>0){ tname += m + '分' }
if(s>0){ tname += s + '秒' }
return {
mTotal: e,
timeList: [h, m, s],
timeName: tname,
}
}
const state = reactive({
stm: '2024-08-13 10:00:00',
etm: '2024-08-13 11:00:03',
ztime: {
timeList: [],
timeName: ''
}
})
onMounted(()=>{
const cha = dayjs(state.etm).diff(state.stm)
state.ztime = getMinuteToFormat(cha/1000)
})
</script>