I've added two Bootstrap Datetime pickers to a HTML5 form. So far I can capture the DateTime value of each picker using the change event of the control.
Now I have a case where the time span difference between both DateTime pickers needs to be stored as a var.
I did come across an example similar to this ask here. But my short implementation below alerts a value of "Nan" instead of the expected time span difference when the actual time picker value is changed.
Question:
How can you calculate the time span between two date picker values in Javascript or JQuery?
Code gist:
var start;
var actual;
$("#OutageStartDisplay").on("dp.change", function (e) {
start = $('#OutageStart').val(moment(e.date).format());
});
//On change of the ActualDisplay datetime picker
//update's the Actual hidden field with the datetime.
$("#ActualDisplay").on("dp.change", function (e) {
actual = $('#Actual').val(moment(e.date).format());
//If the actual is set, calculate the outage duration
var timeDiff = Math.abs(start - actual);
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
alert(diffDays);
});