判断两个时间跨度在三个月内
有的时候 根据项目需求会出现 在结束前三个月内 不需要提交 或者更改内容、为了使信息准确不能简单把3个月转换为90天判断、而是要根据日期判断三个月内。还需要考虑是否在同一年内
我们先创建一个函数 接受三个参数 time1:当前时间、time2:结束日期、m:相差月份
completeDate (time1, time2, m) {
const year1 = time1.getFullYear()
const year2 = time2.getFullYear()
const month1 = time1.getMonth() + 1
const month2 = time2.getMonth() + 1
const day1 = time1.getDate()
const day2 = time2.getDate()
if (year2 === year1) { // 判断两个日期 是否是同年
if (month2 - month1 > m) { // 相差是否 在m个月中
return false
} else if (month2 - month1 === m) { // 如果正好为 m月 判断天数
if (day2 > day1) {
return false
}
}
} else { // 不同年
if (year2 - year1 > 1) {
return false
}
else if (year2 - year1 === 1) {
if (month2 > m || month1 + m - month2 < 12) {
return false
}
else if (month1 + m - month2 === 12) { // 正好为m月 判断天数
if (day2 > day1) {
return false
}
}
}
}
return true
},
调用
this.completeDate(new Date(), new Date(eEndTime), 3)