会计期间选择器:起始日期不能晚于结束日期错误解决方法
{
"title": "<el-form-item label="会计期间:" required>
<el-col :span="8">
<el-form-item prop="startAccountingTime">
<el-date-picker
ref="startPicker"
clearable
size="mini"
v-model="form.startAccountingTime"
type="date"
value-format="yyyy-MM-dd"
placeholder="选择起始日期"
style="width: 130px"
>
<el-col :span="2"> ————
<el-col :span="6">
<el-form-item prop="endAccountingTime">
<el-date-picker
ref="endPicker"
clearable
size="mini"
v-model="form.endAccountingTime"
type="date"
value-format="yyyy-MM-dd"
placeholder="选择结束日期"
style="width: 130px"
>
startAccountingTime: [
{
type: "date"
required: true
message: "请选择会计开始时间"
trigger: "blur"
}
]
endAccountingTime: [
{
type: "date"
required: true
message: "请选择会计结束时间"
trigger: "blur"
validator(rule, value, callback) {
if (
this.form.startAccountingTime &&
this.form.endAccountingTime
) {
const startDate = moment(
this.form.startAccountingTime,
"YYYY-MM-DD"
);
const endDate = moment(
this.form.endAccountingTime,
"YYYY-MM-DD"
);
if (startDate.isAfter(endDate)) {
callback(new Error("起始日期不能晚于结束日期"));
} else {
callback();
}
} else {
callback();
}
}
}
],: dateObject.getTime is not a function报错解决内容:这个报错是因为在比较日期时,使用了dateObject.getTime()的方式,但是dateObject并不是一个Date对象,所以getTime方法不存在。解决这个问题可以通过以下步骤:
- 确保
this.form.startAccountingTime和this.form.endAccountingTime是正确的日期对象。你可以在代码中打印这两个变量,确保它们是Date对象。 - 如果这两个变量不是Date对象,需要将它们转换为Date对象。你可以使用
new Date()来创建Date对象,例如new Date(this.form.startAccountingTime)和new Date(this.form.endAccountingTime)。 - 在比较日期时,可以使用
getTime()方法获取日期的时间戳,然后进行比较。例如,startDate.getTime()和endDate.getTime()。 - 最后,记得在验证器函数中调用回调函数来返回验证结果。如果日期比较不符合条件,可以传递一个Error对象给回调函数,否则传递空参数。
以下是修改后的代码示例:
endAccountingTime: [
{
type: \"date\"
required: true
message: \"请选择会计结束时间\"
trigger: \"blur\"
validator(rule, value, callback) {
if (this.form.startAccountingTime && this.form.endAccountingTime) {
const startDate = new Date(this.form.startAccountingTime);
const endDate = new Date(this.form.endAccountingTime);
if (startDate.getTime() > endDate.getTime()) {
callback(new Error(\"起始日期不能晚于结束日期\"));
} else {
callback();
}
} else {
callback();
}
}
}
],
确保按照上述步骤修改代码后,再次运行程序,应该就不会再报dateObject.getTime is not a function错误了。
原文地址: http://www.cveoy.top/t/topic/qyaf 著作权归作者所有。请勿转载和采集!