vue系列--【根据开始日期 ,期限,计算出结束日期(处理到期日是2.29日且到期年不是闰年的情况)】

<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>        
 

  

posted on 2024-03-01 16:19  码农小小海  阅读(18)  评论(0编辑  收藏  举报

导航