Skip to content

Instantly share code, notes, and snippets.

@ash-f
Created September 5, 2022 21:08
Show Gist options
  • Save ash-f/b2baf102cf6dcb779297bb181d9e3650 to your computer and use it in GitHub Desktop.
Save ash-f/b2baf102cf6dcb779297bb181d9e3650 to your computer and use it in GitHub Desktop.
fluent-forms-birthdate-calc
function calcAge($dob,$age){
$dob.on('change', function() {
var dobary = $(this).val().split('/');
var dobobj = new Date(dobary[2], dobary[0]-1, dobary[1]), birthday = { year: dobobj.getFullYear(), month: dobobj.getMonth(), date: dobobj.getDate() };
var todayobj = new Date(), thisyear = todayobj.getFullYear();
var thisYearsBirthday = new Date(thisyear, birthday.month-1, birthday.date);
var age = thisyear - birthday.year;
if(todayobj < thisYearsBirthday){ age--; }
$age.val(age);
});
}
function bindEvents() {
var $dob1 = $form.find('#ff_3_date_of_birth');
var $age1 = $form.find('#ff_3_age');
calcAge($dob1,$age1);
}
bindEvents();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment