Reply To: Calculate Age from Date of Birth

Home Forums Quform WordPress Calculate Age from Date of Birth Reply To: Calculate Age from Date of Birth

#29149

justdiveworld
Participant

Hi Ally,

Thanks a million.

I need it in a text field and not hidden field, would you change $hiddenField to $textField to look like the code below?

3_203 is our date field
3_110 is our text field

I haven’t been able to get it to work:

jQuery(function ($) {
var $dateField = $('.quform-field-3_103'),
$textField = $('.quform-field-3_110'),
datePicker = $dateField.data('kendoDatePicker'),
options = $dateField.data('options') || {},
culture = options.locale || 'en-US',
format;

if (window.kendo && datePicker) {
format = options.format ? options.format : kendo.getCulture(options.locale).calendars.standard.patterns.d;

$dateField.on('keyup blur', onDateChange);
datePicker.bind('change', onDateChange);
}

function onDateChange() {
var value = $dateField.val(),
age = '';

if (Quform.isNonEmptyString(value)) {
var date = kendo.parseDate(value, format, culture);

if (date !== null) {
age = getAge(date);
}
}

$textField.val(age).change();
}

function getAge(birthDate) {
var today = new Date(),
age = today.getFullYear() – birthDate.getFullYear(),
m = today.getMonth() – birthDate.getMonth();

if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age–;
}

return age;
}
});

Thanks a million,

Denis

Be inspired. © 2019 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy