惠企通
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

31 lines
768 B

import { ref, onMounted, onUnmounted } from 'vue';
export default function useTimeDiff(targetDateStr) {
const days = ref(0);
const hours = ref(0);
const minutes = ref(0);
const calculateDiff = () => {
const targetDate = new Date(targetDateStr.replace(/-/g, '/'));
const now = new Date();
const diffMs = now - targetDate;
days.value = Math.max(0, Math.floor(diffMs / 86400000));
hours.value = Math.max(0, Math.floor((diffMs % 86400000) / 3600000));
minutes.value = Math.max(0, Math.floor((diffMs % 3600000) / 60000));
};
let timer;
onMounted(() => {
calculateDiff();
timer = setInterval(calculateDiff, 60000);
});
onUnmounted(() => {
clearInterval(timer);
});
return { days, hours, minutes };
}