使用Vue3和Day.js来计算一个日期,并将结果格式化为YYYY-MM-DD格式
使用npm或yarn来安装day.js
npm install dayjs
# 或者
yarn add dayjs
vue组件中使用
<template>
<div>
{{ formattedDate }}
</div>
</template>
<script>
import { ref, onMounted } from 'vue';
import dayjs from 'dayjs';
export default {
setup() {
const formattedDate = ref('');
onMounted(() => {
const today = dayjs().startOf('day'); // 获取今天的日期并设置到午夜
const twelveDaysAgo = today.subtract(12, 'days'); // 从今天减去12天
const tomorrow = twelveDaysAgo.add(1, 'days'); // 再加一天得到昨天的日期
formattedDate.value = tomorrow.format('YYYY-MM-DD'); // 格式化日期
});
return {
formattedDate
};
}
};
</script>