简体   繁体   English

.diff不是Moment.js中的函数

[英].diff is not a function in Moment.js

I'm trying to get the difference between two dates, but I'm getting the response: Uncaught TypeError: x.diff is not a function On other topics I've seen I have to create a moment object, but for as far as I know, I'm doing that. 我试图获取两个日期之间的差额,但得到响应: Uncaught TypeError: x.diff is not a function在其他主题上,我已经看到我必须创建一个矩对象,但到目前为止我知道,我正在这样做。 Code: 码:

function datecheck(){
  var y = moment($("#input_pickup_date").val(),"L").format("L");
  var x = moment().format("L");
  console.log(x.diff(y, 'days'));
}

Via the docs , moment().format() returns a String, so your x and y variables are both Strings. 通过docsmoment().format()返回一个String,因此您的xy变量均为String。 If you need to both do calculations on and display the values, separate them out into different variables: 如果您既需要进行计算并显示值,则将它们分成不同的变量:

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'));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM