我试图获取两个日期之间的差异,但收到响应:Uncaught TypeError: x.diff is not a function
在我见过的其他主题上,我必须创建一个时刻对象,但据我所知,我正在这样做。
代码:
function datecheck(){
var y = moment($("#input_pickup_date").val(),"L").format("L");
var x = moment().format("L");
console.log(x.diff(y, 'days'));
}
最佳答案
通过 docs , moment().format()
返回一个字符串,因此您的 x
和 y
变量都是字符串。如果您需要计算并显示值,请将它们分成不同的变量:
function datecheck() {
var dateSubmitted = moment($("#input_pickup_date").val(), "L"), //your old x variable
now = moment(), //your old y variable
dateSubmittedDisplay = dateSubmitted.format("L"), //a string representing the submitted date in the format you wanted.
nowDisplay = now.format("L"); //a string representing now in the format you wanted.
console.log(x.diff(y, 'days'));
}
关于javascript - .diff 不是 Moment.js 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52086695/