Last active
July 1, 2023 16:36
-
-
Save ahsannayem/5488a728ff6d5eff9194691b82338951 to your computer and use it in GitHub Desktop.
Date range Calculator For Fluent Forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
let fp = flatpickr("#ff_91_from", {}); // Your Form's ID | |
let fp2 = flatpickr("#ff_91_to", {}); // Your Form's ID | |
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'; | |
$form.find('input[name='+showHere+']').val(diffDays).trigger('change'); | |
} | |
else $form.find('input[name='+showHere+']').val(diffDays).trigger('change'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment