最近项目有一个功能需要计算两短时间之间相隔的条数,写完感觉应记录分享一下!
vue methods方法
// 时间计算
mothTotalDays(year, moth) {
let d = new Date(year, moth, 0);
return d.getDate();
},
// 时间间距
dayGap(bfYear, bfMoth, bfDay, nowYear, nowMoth, nowDay) {
let yearGap = nowYear - bfYear;
let mothGap = nowMoth - bfMoth;
let dayGap = nowDay - bfDay;
let timeList = [];
let sum=0;
if (yearGap == 0) {
if (mothGap == 0) {
return dayGap;
} else if (mothGap > 0) {
timeList.push({year:'',mothList:[]})
timeList[0].year= bfYear;
for (let i = 0; i < mothGap; i++) {
timeList[0].mothList.push(parseInt(bfMoth + i));
}
} else {
return "请确定后者时间大于前者时间";
}
} else if (yearGap > 0) {
for (let i = 0; i <= yearGap; i++) {