簡體   English   中英

.diff不是Moment.js中的函數

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

我試圖獲取兩個日期之間的差額,但得到響應: 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'));
}

通過docsmoment().format()返回一個String,因此您的xy變量均為String。 如果您既需要進行計算並顯示值,則將它們分成不同的變量:

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