Check out the LimeSurvey source code on GitHub!

Age calculation script

More
3 years 2 months ago #101716 by helcom
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 1 month ago #101723 by Ben_V
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
All LS releases => bit.ly/1VMuTDu | 2.06lts => 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 1 month ago #101768 by mfaber
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 1 month ago - 3 years 1 month ago #101769 by mfaber
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 1 month 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 1 month ago #101776 by helcom
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 1 month ago #101778 by helcom
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.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form