Skip to content

Instantly share code, notes, and snippets.

@aimahdi
Last active February 17, 2022 07:13
Show Gist options
  • Save aimahdi/dc5da6257d58dadd9af1b15f44862898 to your computer and use it in GitHub Desktop.
Save aimahdi/dc5da6257d58dadd9af1b15f44862898 to your computer and use it in GitHub Desktop.
var from = "from"; //change the name attribute of the from date field
var to = "to"; //change the name attribute of the to date field
var showHere = "showHere"; //change the name attribute of show difference field
var daysText = "days";
let fp = flatpickr("#ff_225_from", {}); //change form id with yours
let fp2 = flatpickr("#ff_225_to", {}); //change form id with yours
function getDayDiff(input_name_1, input_name_2) {
var $date1 = $form.find('input[name='+input_name_1+']');
var time2 = (new Date()).getTime();
if(input_name_2) {
var $date2 = $form.find('input[name='+input_name_2+']');
var date2Val = $date2.val();
if(!date2Val) {
return 0;
}
time2 = new Date(fp.parseDate(date2Val, $date2.data('format'))).getTime();
}
var date1Val = $date1.val();
if(!date1Val) {
return 0;
}
var time1 = new Date(fp2.parseDate(date1Val, $date1.data('format'))).getTime();
return Math.ceil((time1-time2)/(24*3600*1000));
}
$form.find('input[name='+from+'],input[name='+to+']').on('change', function() {
var diffDays = getDayDiff(to, from);
if(diffDays <= 0) {
diffDays = '0';
}
else $form.find('input[name='+showHere+']').val(diffDays+' '+daysText).trigger('change');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment