ฉันมีวันที่สองวันที่เก็บไว้ใน var (สร้างโดยตัวเลือกวันที่) หากต้องการจัดรูปแบบเหล่านี้ ฉันใช้ moments.js
ฉันต้องการคำนวณจำนวนวันระหว่างสองฟิลด์นี้และเก็บตัวเลขนี้ไว้ใน var days
นี่คือรหัสของฉันจนถึงตอนนี้:
// When someone clicks my submit button, I create an url stored in the var url.
$(".book_submit").on('click', function(e){
// I get the datepicker and input fields data
var from = $('.from').val();
var to = $('.to').val();
// I can format the "from" var the way I want
from = moment(from, "DD.MM.YYYY");
from = moment(from).format("YYYY-MM-DD");
// I can format the "to" var the way I want
to = moment(to, "DD.MM.YYYY");
to = moment(to).format("YYYY-MM-DD");
// I want to store the number between the two dates in the var "days", to place it inside my link below. I have found ".diff(to, 'days') in the moments documentation.
//days = from.diff(to, 'days'); // =1
var url = "https://mycustomlink&days= " + days + "";
});
ฉันไม่สามารถให้ from.diff(to, 'days')
ทำงานและเก็บไว้ในตัวแปรได้ ฉันได้ลองตั้งค่าดังนี้:
var days = from.diff(to, 'days');
ฉันจะเก็บความแตกต่างระหว่างวันนี้ไว้ใน var days
ของฉันได้อย่างไร ฉันขอขอบคุณข้อเสนอแนะใด ๆ