**小需求:element ui关于el-date-picker在实际开发过程中需要限制日期,比如:开始日期不能比结束日期迟,结束日期-开始日期不能相差超过365天。
话不多少,上代码看看。**
<template>
<div class="home">
<HelloWorld msg="主页" />
<div>
<el-date-picker v-model="startDate" type="date" placeholder="开始日期" value-format="yyyy-mm-dd">
</el-date-picker>
<span>-</span>
<el-date-picker v-model="endDate" type="date" placeholder="结束日期" value-format="yyyy-mm-dd">
</el-date-picker>
<button @click="findList">查询</button>
</div>
</div>
</template>
<script>
import HelloWorld from "@/components/HelloWorld.vue";
export default {
name: "HomeView",
components: {
HelloWorld,
},
data() {
return {
startDate: "",
endDate: "",
};
},
methods: {
findList() {
console.log("查询",this.startDate,this.endDate);
if (!this.startDate) {
this.$message("请选择开始日期!");
return;
}
if (!this.endDate) {
this.$message("请选择结束日期!");
return;
}
var sDate, eDate, sDate2, eDate2, iDate;
sDate = this.startDate.split("-");
sDate2 = new Date(`${sDate[1]+1}/${sDate[2]}/${sDate[0]}`);
eDate = this.endDate.split("-");
eDate2 = new Date(`${eDate[1]+1}/${eDate[2]}/${eDate[0]}`);
iDate = parseInt(Math.abs(eDate2.getTime() - sDate2.getTime()) / 1000 / 60 / 60 / 24);
console.log("相差的天数",iDate);
if(iDate>"365"){
this.$message("日期区域不能超过365天!");
return;
}
if(eDate2.getTime() - sDate2.getTime()<0){
this.$message("结束日期不能大于开始日期!");
return;
}
......
},
},
};
</script>
|