Age calculation script

More
3 years 10 months ago #101716 by helcom
helcom created the topic: Age calculation script
Hi everyone.

I have been trying to utilise a script posted elsewhere in these forums to try and calculate the age of an applicant.

I have two questions, Q1 being a Date of Birth question using Date (popup) question type. Q2 is a short text question type with the code included below.

The Problem:
When I select a date from the popup the letters NaN appear in the short text question below instead of their age.

I am NOT a javascript proficient person so any help will be GREATLY appreciated.


See pasted code below:
***start of pasted code***
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$('.date input.popupdate').change(function() {
// Format: mm-dd-yyyy
var dob= $('.date input.popupdate').val();
var dobParts = dob.split("-");
dob = dobParts[0] + '/' + dobParts[1] + '/' + dobParts[2];
var dobDate= new Date(dob);
var birth_year = dobDate.getYear();
var birth_month =dobDate.getMonth();
var birth_day =dobDate.getDate();
//alert ("you set the dob (mm-dd-yyyy) to: "+(dobDate.getMonth()+1)+"-"+dobDate.getDate()+"-"+dobDate.getFullYear());
var today_date = new Date();
var today_year = today_date.getYear();
var today_month = today_date.getMonth();
var today_day = today_date.getDate();
var age = Number(today_year - birth_year);
if ( today_month < birth_month) {
age--;
}
else if ((birth_month == today_month) && (today_day < birth_day)) {
age--;
}
$('.text-short:eq(0) input.text').val(age);
});
});

</script>
***end of pasted code***

Thanks

Please Log in to join the conversation.

More
3 years 10 months ago #101723 by Ben_V
Ben_V replied the topic: Age calculation script
Hi,

Your "NaN" issue is probably coming from your default date format (dd-mm-yy etc.)

BTW, you may try this approach based on an equation type question.
=>sample survey (.lss file) available here
.

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
The following user(s) said Thank You: helcom

Please Log in to join the conversation.

More
3 years 10 months ago #101768 by mfaber
mfaber replied the topic: Age calculation script
Please have a look here, this solution is probably MUCH easier:
manual.limesurvey.org/Expression_Manager...ce_between_two_dates

There's also a Demo survey to download. Please note that you need a fairly recent Limesurvey version (I think mid July or later is fine).
The following user(s) said Thank You: helcom

Please Log in to join the conversation.

More
3 years 10 months ago - 3 years 10 months ago #101769 by mfaber
mfaber replied the topic: Age calculation script
In your case, you may just write {(strtotime('now')-strtotime(Q1))/60/60/24/365} in an euqation type question, then you also do not ask about the current date.
Last Edit: 3 years 10 months ago by mfaber. Reason: calculate YEARS not days :)
The following user(s) said Thank You: helcom

Please Log in to join the conversation.

More
3 years 10 months ago #101776 by helcom
helcom replied the topic: Age calculation script
Thanks for all your responses, I do appreciate it.

We are running an older version of Limesurvey due to the massive customisation completed 3 years ago so we don't have the option of an Equation Question Type. An upgrade of our system is on the Roadmap for next year but stuck with the old system for now.

Please Log in to join the conversation.

More
3 years 10 months ago #101778 by helcom
helcom replied the topic: Age calculation script
Hi all,

Thanks to everyone for their time to respond. I have since found the issue and resolved it by changing the default date fromat from dd-mm-yyyy to mm-dd-yyyy.

Thanks again everyone.

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now