VUE 计算两个日期的月份差,不足一个月(并且大于3天)按一个月算
cal() { if (this.model.contractStartDate && this.model.contractEndDate) { let EndDate = this.model.contractEndDate.split('-'); let StartDate = this.model.contractStartDate.split('-'); let dayDate = parseInt(EndDate[2]) - parseInt(StartDate[2]); console.log(EndDate); console.log(StartDate); console.log(dayDate); EndDate = parseInt(EndDate[0]) * 12 + parseInt(EndDate[1]); StartDate = parseInt(StartDate[0]) * 12 + parseInt(StartDate[1]); if (dayDate > 3) { this.model.leaseTime = EndDate - StartDate + 1; } else { this.model.leaseTime = EndDate - StartDate; } } },
注意: 这里大于3,是因为月份可能有28天,也可能为31天
.split(‘-’) 按照‘-’ 裁剪成数组
/ 裁剪效果 / ['2023', '04', '13'] ['2023', '04', '05']
VUE 通过开始日期、经历月份 ,获取结束日期
cal111(){ if (this.model.contractStartDate && this.model.leaseTime) { let StartDate = this.model.contractStartDate.split('-'); let month = parseInt(StartDate[1]) + parseInt(this.model.leaseTime); let year = month/12 let months = month%12 this.model.contractEndDate = parseInt(StartDate[0])+year + "-" + months + "-" + StartDate[2] } },