I have the following code and need to calculate the difference between the two dates but why am I getting diff is not a function? Note I am using pikaday.js and moment.js but I have hardcoded the inputs for simplicity. I have seen other similar posts on StackOverflow but I am new to moment.js and need help with the syntax and not just a short sentence as an answer. Thank you in advance.
<div>ARRIVAL</div>
<div>
<input type="text" id="txtArrival" value="07/12/18">
</div>
<div>DEPARTURE</div>
<div>
<input type="text" id="txtDeparture" value="15/12/18">
</div>
var aDate, dDate, a, b, days;
//get arrival and departure
aDate = new Pikaday({
field: document.getElementById('txtArrival'),
format: 'D MMM YYYY',
onSelect: function() {
console.log(aDate.getMoment().format('DD/MM/YY'));
}
});
dDate = new Pikaday({
field: document.getElementById('txtDeparture'),
format: 'D MMM YYYY',
onSelect: function() {
console.log(dDate.getMoment().format('DD/MM/YY'));
}
});
//calculate days
a = aDate.getMoment().format('DD/MM/YYYY');
d = dDate.getMoment().format('DD/MM/YYYY');
days = d.diff(a, 'days');
format()
returns string
thus the error is expected. You need to use moment
object and perform .diff()
on them
Use
days = dDate.getMoment().diff(aDate.getMoment(), 'days');
instead of
days = d.diff(a, 'days');