Skip to content

Instantly share code, notes, and snippets.

@star26bsd
Forked from alex-authlab/fluent form age calculate
Last active November 6, 2022 09:16
Show Gist options
  • Save star26bsd/f4ce7ac78021456b32fea59159b01d58 to your computer and use it in GitHub Desktop.
Save star26bsd/f4ce7ac78021456b32fea59159b01d58 to your computer and use it in GitHub Desktop.
Birthday Calculation in Fluent Form
// Age calculator
// 1. First take a Date picker & a text input field
// 2. In the text input element, add the element class 'age'
// 3. Add a custom element class 'date' in the date picker, then add the following js in your fluent form custom js
jQuery( ".date" ).change(function() {
var age = getAge(jQuery(this).val())
jQuery('.age').val( age)
});
function getAge(DOB) {
//when date format dd.mm.yyyy
birthDate = DOB.split(".");
currentDate = new Date();
// JS starts counting months from 0, so add +1
var years = (currentDate.getFullYear() - birthDate[2]);
if (currentDate.getMonth()+1 < parseInt(birthDate[1]) || currentDate.getMonth()+1 == parseInt(birthDate[1]) && currentDate.getDate() < birthDate[0]) {
years--;
}
if(years < 0) {
years = 0;
}
return years;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment