日常开发中,会经常用到起止日期比较大小,这里会将时间都转化为时间戳,方便进行比较,
以element-ui的日期选择组件为例
如图:在form表单中
html代码如下:
<el-form-item label="Start Date" prop="StartDate">
<el-date-picker
v-model="StartDate"
format="yyyy/MM/dd"
value-format="yyyy/MM/dd"
@change="changeDate('start')"
>
</el-date-picker>
</el-form-item>
<el-form-item label="End Date" prop="EndDate">
<el-date-picker
v-model="EndDate"
format="yyyy/MM/dd"
value-format="yyyy/MM/dd"
@change="changeDate('end')"
>
</el-date-picker>
</el-form-item>
js代码如下
changeDate(type) {
if (this.StartDate && this.StartDate!== "" && this.EndDate && this.EndDate!== "") {
if (new Date(this.StartDate).getTime() > new Date(this.EndDate).getTime()) {
this.$message({
type: 'warning',
message: "End Date 必须大于 start Date",
duration: 4000
});
if (type === 'start') {
this.StartDate = "";
} else {
this.EndDate = "";
}
}
}
},