- Posts: 20
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Calculate age in an Equation type question, using Date of Birth
- tfj
- Topic Author
- Offline
- Junior Member
Less
More
11 years 7 months ago #84840
by tfj
Replied by tfj on topic Calculate age in an Equation type question, using Date of Birth
Denis, benitov, tpartner:
Many thanks to each of you! My limited knowledge of javascript and upcoming deadlines make for a lethal combination! I am very thankful for this forum and your help!
tfj
Many thanks to each of you! My limited knowledge of javascript and upcoming deadlines make for a lethal combination! I am very thankful for this forum and your help!
tfj
The topic has been locked.
- tpartner
- Away
- LimeSurvey Community Team
Less
More
- Posts: 10632
- Thank you received: 3696
11 years 7 months ago #84841
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Calculate age in an Equation type question, using Date of Birth
You're welcome, I'm glad we could help.
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- tfj
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 20
- Thank you received: 0
11 years 7 months ago #84933
by tfj
Replied by tfj on topic Calculate age in an Equation type question, using Date of Birth
We have been testing this solution and have found something strange when using Internet Explorer 8. When calculating the age for a person with a Date of Birth prior to 2000, the answer for age puts a "19" in front of the actual age. For instance, if we put "June 10, 1985" for the date of birth, we get 1927" instead of "27."
In Firefox, this works perfectly.
I figure it must have something to do with a setting within Internet Explorer, and not a problem with the code. If so, we may not be able to use the solution since there are so many users in the field.
Any thoughts?
Thanks!
tfj
In Firefox, this works perfectly.
I figure it must have something to do with a setting within Internet Explorer, and not a problem with the code. If so, we may not be able to use the solution since there are so many users in the field.
Any thoughts?
Thanks!
tfj
The topic has been locked.
- TMSWhite
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 84
- Thank you received: 21
11 years 7 months ago #84934
by TMSWhite
If you depend upon LimeSurvey or the support team, please give generously to our 2012 fundraiser .
Replied by TMSWhite on topic Calculate age in an Equation type question, using Date of Birth
Date math is such a common need that we really ought to add date-related functions to Expression Manager, such as day(), month(), year(), age(date_of_birth [, today]), etc.
If you depend upon LimeSurvey or the support team, please give generously to our 2012 fundraiser .
- Expression Manager (EM):
Documentation
|
Sample Surveys
|
How Tos
|
Show Logic File
|
Upgrading to 1.92+ - Excel Style for Survey Structure Import/Export
The topic has been locked.
- tpartner
- Away
- LimeSurvey Community Team
Less
More
- Posts: 10632
- Thank you received: 3696
11 years 6 months ago #85032
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Calculate age in an Equation type question, using Date of Birth
tfj, I don't have IE8 here to test but try changing:
To:
Code:
var age = (today_year) - birth_year;
To:
Code:
var age = Number(today_year - birth_year);
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- tpartner
- Away
- LimeSurvey Community Team
Less
More
- Posts: 10632
- Thank you received: 3696
11 years 6 months ago #85033
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Calculate age in an Equation type question, using Date of Birth
Great idea Tom.Date math is such a common need that we really ought to add date-related functions to Expression Manager...
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- tfj
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 20
- Thank you received: 0
11 years 6 months ago #85043
by tfj
Replied by tfj on topic Calculate age in an Equation type question, using Date of Birth
I changed the line to:
var age = Number(today_year - birth_year);
The calculation still places a "19" to the front of the age in Internet Explorer 8 (for dates of birth prior to 2000), but not in Internet Explorer 9 or Firefox 14. For example, the age may be calculated as 1929 instead of 29. For the years 2000 and later, the age is calculated correctly.
For now, in order to meet my deadline, I added the two questions (Date of birth and age) in one of our surveys, but hid the age question and commented out the code. That way, in case a script fix comes up, I can put it in without deactivating the survey. I am still calculating the age after-the-fact with a MySQL script.
Thanks, all!
tfj
var age = Number(today_year - birth_year);
The calculation still places a "19" to the front of the age in Internet Explorer 8 (for dates of birth prior to 2000), but not in Internet Explorer 9 or Firefox 14. For example, the age may be calculated as 1929 instead of 29. For the years 2000 and later, the age is calculated correctly.
For now, in order to meet my deadline, I added the two questions (Date of birth and age) in one of our surveys, but hid the age question and commented out the code. That way, in case a script fix comes up, I can put it in without deactivating the survey. I am still calculating the age after-the-fact with a MySQL script.
Thanks, all!
tfj
The topic has been locked.
- tfj
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 20
- Thank you received: 0
11 years 6 months ago #85210
by tfj
Replied by tfj on topic Calculate age in an Equation type question, using Date of Birth
I believe I found a solution, after much digging in PHP reference manuals, web searches, etc. I am now able to perform date calculations using the Expression Manager. I modified an example given in PHP and MySQL Web Development, Fourth Edition, by Welling and Thomson (pp. 477-478) (to give proper credit).
Here's what I did:
1) Created a Date of Birth question, of type "Date." The question code I used was "DateofBirth," which you will see in step #2 below. For our purposes, all dates are mm-dd-yyyy.
2) In a separate group that follows the Date of Birth question, I made up an Equation type question, with the following in the "Question" box:
{floor((time() - mktime(0,0,0,substr(DateofBirth,5,2),substr(DateofBirth,8,2),substr(DateofBirth,0,4)))/(365.25*24*60*60))}
This question may be hidden, depending upon your particular needs.
If you need to calculate days, then take out the "365.25" in the script, and so on.
So far in my testing, this seems to be working, but someone may find an improvement, etc. If so, I would appreciate your sharing it.
One other note: the more I use the new Expression Manager, the more I appreciate it.
tfj
Here's what I did:
1) Created a Date of Birth question, of type "Date." The question code I used was "DateofBirth," which you will see in step #2 below. For our purposes, all dates are mm-dd-yyyy.
2) In a separate group that follows the Date of Birth question, I made up an Equation type question, with the following in the "Question" box:
{floor((time() - mktime(0,0,0,substr(DateofBirth,5,2),substr(DateofBirth,8,2),substr(DateofBirth,0,4)))/(365.25*24*60*60))}
This question may be hidden, depending upon your particular needs.
If you need to calculate days, then take out the "365.25" in the script, and so on.
So far in my testing, this seems to be working, but someone may find an improvement, etc. If so, I would appreciate your sharing it.
One other note: the more I use the new Expression Manager, the more I appreciate it.
tfj
The topic has been locked.
- Ben_V
- Offline
- Platinum Member
Less
More
- Posts: 1223
- Thank you received: 351
11 years 6 months ago #85320
by Ben_V
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)
Replied by Ben_V on topic Calculate age in an Equation type question, using Date of Birth
Very nice solution...
Maybe the improvment could be to get it working with the 2 questions in a same group
I attach the sample survey I've created to make a try.
Thank you to share...
Ben/
Maybe the improvment could be to get it working with the 2 questions in a same group
I attach the sample survey I've created to make a try.
Thank you to share...
Ben/
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)
Attachments:
The topic has been locked.
- PawsInMotion
- Offline
- New Member
Less
More
- Posts: 1
- Thank you received: 0
11 years 2 months ago #91050
by PawsInMotion
Replied by PawsInMotion on topic Calculate age in an Equation type question, using Date of Birth
These have all been incredibly helpful! Thank you!
Here's a trick I'm trying to figure out though (without knowing perl or much scripting at all):
How do I show year and month (and, optionally, days) from birth as chosen in the previous question.
Some of my clients aren't a year old yet (dogs) or are a year and some months.
I appreciate the help. I'll keep noodling with this code. At least I figured out that 'floor' means to round the answer.
{floor((time() - mktime(0,0,0,substr(DateofBirth,5,2),substr(DateofBirth,8,2),substr(DateofBirth,0,4)))/(365.25*24*60*60))}
Here's a trick I'm trying to figure out though (without knowing perl or much scripting at all):
How do I show year and month (and, optionally, days) from birth as chosen in the previous question.
Some of my clients aren't a year old yet (dogs) or are a year and some months.
I appreciate the help. I'll keep noodling with this code. At least I figured out that 'floor' means to round the answer.
{floor((time() - mktime(0,0,0,substr(DateofBirth,5,2),substr(DateofBirth,8,2),substr(DateofBirth,0,4)))/(365.25*24*60*60))}
The topic has been locked.
- tfj
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 20
- Thank you received: 0
11 years 2 months ago #91155
by tfj
Replied by tfj on topic Calculate age in an Equation type question, using Date of Birth
PawsInMotion:
The code I used is modular in the sense that you can substitute values in it to come up with whatever type of date measurement you need.
For instance, instead of using the following for years:
{floor((time() - mktime(0,0,0,substr(DateofBirth,5,2),substr(DateofBirth,8,2),substr(DateofBirth,0,4)))/(365.25*24*60*60))}
you can change it to:
{floor((time() - mktime(0,0,0,substr(DateofBirth,5,2),substr(DateofBirth,8,2),substr(DateofBirth,0,4)))/(24*60*60))}
for the number of days. From there, you could use Equation-type questions to come up with months and days, etc.
I tested this on a couple of cases and they seemed okay. Please let me know if your experience is different.
tfj
The code I used is modular in the sense that you can substitute values in it to come up with whatever type of date measurement you need.
For instance, instead of using the following for years:
{floor((time() - mktime(0,0,0,substr(DateofBirth,5,2),substr(DateofBirth,8,2),substr(DateofBirth,0,4)))/(365.25*24*60*60))}
you can change it to:
{floor((time() - mktime(0,0,0,substr(DateofBirth,5,2),substr(DateofBirth,8,2),substr(DateofBirth,0,4)))/(24*60*60))}
for the number of days. From there, you could use Equation-type questions to come up with months and days, etc.
I tested this on a couple of cases and they seemed okay. Please let me know if your experience is different.
tfj
The topic has been locked.
- DWS
- Offline
- New Member
Less
More
- Posts: 15
- Thank you received: 0
10 years 8 months ago #97877
by DWS
Replied by DWS on topic Calculate age in an Equation type question, using Date of Birth
the example works fine... but if yo hide the question then you can't use the value on following questions
The topic has been locked.