Age calculation script

Plus d'informations
il y a 3 ans 8 mois #101716 par helcom
helcom a créé le sujet : 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

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 8 mois #101723 par Ben_V
Ben_V a répondu au sujet : 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)
Les utilisateur(s) suivant ont remercié: helcom

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 8 mois #101768 par mfaber
mfaber a répondu au sujet : 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).
Les utilisateur(s) suivant ont remercié: helcom

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 8 mois - il y a 3 ans 8 mois #101769 par mfaber
mfaber a répondu au sujet : 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.
Dernière édition: il y a 3 ans 8 mois par mfaber. Raison: calculate YEARS not days :)
Les utilisateur(s) suivant ont remercié: helcom

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 8 mois #101776 par helcom
helcom a répondu au sujet : 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.

Connexion pour participer à la conversation.

Plus d'informations
il y a 3 ans 8 mois #101778 par helcom
helcom a répondu au sujet : 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.

Connexion pour participer à la conversation.

Avez-vous déjà participé à notre enquête auprès des clients?

Ne manquez pas votre chance à des prix incroyables.

S'il vous plaît cliquez ici pour participer:

Commencer

Commencez dès maintenant !

Créez simplement un compte et commencez à utiliser LimeSurvey dès aujourd'hui.

Inscrivez-vous maintenant