Forked from alex-authlab/fluent form age calculate
Last active
November 6, 2022 09:16
-
-
Save star26bsd/f4ce7ac78021456b32fea59159b01d58 to your computer and use it in GitHub Desktop.
Birthday Calculation in Fluent Form
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
// 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