Check out the LimeSurvey source code on GitHub!
Welcome, Guest
Username: Password:

TOPIC: Age calculation script

Age calculation script 2 years 10 months ago #101716

  • helcom
  • helcom's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
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
The administrator has disabled public write access.

Age calculation script 2 years 10 months ago #101723

  • Ben_V
  • Ben_V's Avatar
  • Offline
  • Platinum Lime
  • Posts: 1826
  • Thank you received: 449
  • Karma: 111
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 => bit.ly/20NW9V8 (already included in /docs/demosurveys)
The administrator has disabled public write access.
The following user(s) said Thank You: helcom

Age calculation script 2 years 10 months ago #101768

  • mfaber
  • mfaber's Avatar
  • Offline
  • Expert Lime
  • Posts: 86
  • Thank you received: 22
  • Karma: 4
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 administrator has disabled public write access.
The following user(s) said Thank You: helcom

Age calculation script 2 years 10 months ago #101769

  • mfaber
  • mfaber's Avatar
  • Offline
  • Expert Lime
  • Posts: 86
  • Thank you received: 22
  • Karma: 4
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: 2 years 10 months ago by mfaber. Reason: calculate YEARS not days :)
The administrator has disabled public write access.
The following user(s) said Thank You: helcom

Age calculation script 2 years 10 months ago #101776

  • helcom
  • helcom's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
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.
The administrator has disabled public write access.

Age calculation script 2 years 10 months ago #101778

  • helcom
  • helcom's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
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.
The administrator has disabled public write access.
Time to create page: 0.353 seconds
Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form