<template>
<div>
<p>起息日:{{ startDate }}</p >
<p>最后到期日:{{ endDate }}</p >
</div>
</template>
<script>
import moment from 'moment'; // 引入moment
export default {
data() {
return {
startDate: '2024-02-29',
term:2, // 期限
type:'M, // 单位m是月,Y是年
endDate: ''
};
},
mounted() {
this.calculateEndDate();
},
methods: {
calculateEndDate() {
const startDate = moment(this.startDate);
if(type == 'M'){
const endDate = startDate.add(this.term, 'months');
}else if(type == 'Y'){
const endDate = startDate.add(this.term, 'years');
}
// 最后到期日是闰年的2月29日,但最后到期日当年不是闰年,则取前一天作为最后到期日
if (endDate.month() === 1 && endDate.date() === 29) {
endDate.subtract(1, 'day');
}
this.endDate = endDate.format('YYYY-MM-DD');
}
}
};
</script>