# Calculating age at a certain date

5 years 1 month ago #96089 by ahchouch
ahchouch created the topic: Calculating age at a certain date
Hi Everybody
It's my first use of Limesurvey and I know nothing about Javascript!!
I have a question group including these question:
Q1: date question (dd/mm/yyyy): date of birth
Q2; date question : date of hospitalization
Q3: date question : date of intervention
I need to calculate age (in the same group) at the intervention date (DOB - DOI: in years)
I also need to verify before that:
the hospitalization date is >= to the DOB
the intervention date is >= to the hospitalization date
If one of these condition is not OK an alert should be given (according to the unsatisfied condition)and the age wouldn't be calculated than.

5 years 1 month ago #96090 by Ben_V
Ben_V replied the topic: Calculating age at a certain date
Hello,
A part of expected responses to your questions can be found here

The topic teach how to calculate age (in an equation type question) from dob... or from any retrieved date

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)

5 years 1 month ago - 5 years 1 month ago #96108 by ahchouch
ahchouch replied the topic: Calculating age at a certain date
Thanks Ben
I've alredy had a look at that and tried it. It works with today date as differential of DOB, but when I replace it with the DOI (date of intervention) it doesn't work.
I guess it has something to do with the listener :\$('#answer48427X164X1612').change(function() {
I don't know if it has to be changed (and how) to listen to the change of the tow dates (DOB and DOI).
What do you think about it?
I attach the group question ( I added the second question value to the change listener!)
Could tell me what's wrong?

#### File Attachment:

File Name: limesurvey...p_35.lsg
File Size:5 KB

To compare dates I found this:http://www.limesurvey.org/fr/forum/can-i-do-this-with-limesurvey/93224-validating-dates
Again I don't know how to combine all this to do do all the job (comparing dates and calculating age!)
##### Attachments:
Last Edit: 5 years 1 month ago by ahchouch.

5 years 1 month ago #96114 by ahchouch
ahchouch replied the topic: Calculating age at a certain date
It's OK. There were extra quotes (') in the listener part. Now it works fine to calculate age.
Could someone help me with integrating the comparing dates script to it.
Thanks

5 years 1 month ago #96115 by Ben_V
Ben_V replied the topic: Calculating age at a certain date
The problem is to adapt this code to use the date at intervention instead the current date.

It looks possible and I'll make some try when I get a bit more time but if I don't start very confident because I used to face issues when manipulating dates

For your other questions maybe you can calculate the number of days between the 2 date s and if the returned value is negative, an alert may be displayed

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)

5 years 1 month ago #96116 by Ben_V
Ben_V replied the topic: Calculating age at a certain date
More resource:

1) external

(search for "Calculating date difference" at the end)

3) sample survey (if you can install it...)

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)

5 years 1 month ago #96182 by ahchouch
ahchouch replied the topic: Calculating age at a certain date
thank you Ben.
I actually used the script as in the attached example. For comparing dates and getting alerts, simple (if condition) were sufficiant.
here's the group with the three date questions (dob, doi, doh) and the age calculation.

#### File Attachment:

File Name: limesurvey...up_2.lsg
File Size:9 KB
##### Attachments:

5 years 1 month ago #96261 by ahchouch
ahchouch replied the topic: Calculating age at a certain date
Hi
When I try to compare dates that aren't on the same page, it doesn't work.
I think it's a problem of reference to the date that isn't on the current page. I don't know how to refer to it. Seems it's not possible with JS?
How could I than calculate difference and get alerts i_n that case?
Any ideas!