使用 momentjs 寻找差异时的 NaN
NaN while finding Difference using momentjs
我正在使用 flatpickr
从用户那里获取日期。我从用户那里得到两个日期。现在我想找出两个日期之间的差异,但它显示 NaN
。
computed: {
total() {
if (this.selectedRooms && this.selectedRooms.length) {
const check_in = moment(this.form.check_in, "d-m-Y H:i");
const check_out = moment(this.form.check_out, "d-m-Y H:i");
const nights = check_in.diff(check_out, "days");
let total = 0;
for (let i = 0; i < this.selectedRooms.length; i++) {
if (this.invoice.nights) {
total += +this.selectedRooms[i][this.type] * +nights;
} else {
total += +this.selectedRooms[i][this.type];
}
}
return total;
} else {
return 0;
}
}
}
Flatpickr 配置
configdateTimePicker: {
enableTime: true,
dateFormat: "d-m-Y H:i",
},
错误
NaN
当我在不解析日期的情况下尝试它时,它显示无效日期
Formatting/parsing 令牌在 flatpickr
and moment
. So when parsing to moment object, you must follow moment formatting/parsing token
中不同
const date1 = "25-08-2020 21:38"
const date2 = "26-08-2020 21:38"
const firstDate = moment(date1, "DD-MM-YYYY HH:mm")
const secondDate = moment(date2, "DD-MM-YYYY HH:mm")
console.log(firstDate.diff(secondDate, "days"))
<script src="https://momentjs.com/downloads/moment.min.js"></script>
查找日期差异不需要格式化。您可以使用以下代码。
const date1 = '2020-08-25 12:00:00';
const date2 = '2020-08-26 12:00:00';
const firstDate = moment(date1);
const secondDate = moment(date2);
console.log(firstDate.diff(secondDate, "days"))
我正在使用 flatpickr
从用户那里获取日期。我从用户那里得到两个日期。现在我想找出两个日期之间的差异,但它显示 NaN
。
computed: {
total() {
if (this.selectedRooms && this.selectedRooms.length) {
const check_in = moment(this.form.check_in, "d-m-Y H:i");
const check_out = moment(this.form.check_out, "d-m-Y H:i");
const nights = check_in.diff(check_out, "days");
let total = 0;
for (let i = 0; i < this.selectedRooms.length; i++) {
if (this.invoice.nights) {
total += +this.selectedRooms[i][this.type] * +nights;
} else {
total += +this.selectedRooms[i][this.type];
}
}
return total;
} else {
return 0;
}
}
}
Flatpickr 配置
configdateTimePicker: {
enableTime: true,
dateFormat: "d-m-Y H:i",
},
错误
NaN
当我在不解析日期的情况下尝试它时,它显示无效日期
Formatting/parsing 令牌在 flatpickr
and moment
. So when parsing to moment object, you must follow moment formatting/parsing token
const date1 = "25-08-2020 21:38"
const date2 = "26-08-2020 21:38"
const firstDate = moment(date1, "DD-MM-YYYY HH:mm")
const secondDate = moment(date2, "DD-MM-YYYY HH:mm")
console.log(firstDate.diff(secondDate, "days"))
<script src="https://momentjs.com/downloads/moment.min.js"></script>
查找日期差异不需要格式化。您可以使用以下代码。
const date1 = '2020-08-25 12:00:00';
const date2 = '2020-08-26 12:00:00';
const firstDate = moment(date1);
const secondDate = moment(date2);
console.log(firstDate.diff(secondDate, "days"))