Welcome, Guest
Username: Password: Remember me

TOPIC: Age calculation script

Age calculation script 10 months 1 week 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 10 months 1 week ago #101723

  • Ben_V
  • Ben_V's Avatar
  • OFFLINE
  • Platinum Lime
  • Posts: 1098
  • Thank you received: 247
  • Karma: 78
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

goo.gl/Bw5iM => Recherche GG dans le forum français (remplacer "exemple" dans la barre de recherche)
goo.gl/WX8PH => GG search for english forum (Replace "example" in the search bar)
goo.gl/IxiGu => Búsqueda en el foro en español (Cambiar "ejemplo" en la barra de...
The administrator has disabled public write access.
The following user(s) said Thank You: helcom

Age calculation script 10 months 1 week ago #101768

  • mfaber
  • mfaber's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 85
  • 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 10 months 1 week ago #101769

  • mfaber
  • mfaber's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 85
  • 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: 10 months 1 week 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 10 months 1 week 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 10 months 1 week 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.
Moderators: ITEd
Time to create page: 0.155 seconds
Donation Image