Welcome, Guest
Username: Password: Remember me

TOPIC: Calculate age in an Equation type question, using Date of Birth

Re: Calculate age in an Equation type question, using Date of Birth 2 years 2 months ago #84838

  • Ben_V
  • Ben_V's Avatar
  • OFFLINE
  • Platinum Lime
  • Posts: 1122
  • Thank you received: 255
  • Karma: 79
Thanks Tony...
Tested and working now for me, with all LS versions.
I'm sure that this code will be useful for other people.
Obviously using "more generic selectors" is a great idea !
Ben/
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.

Re: Calculate age in an Equation type question, using Date of Birth 2 years 2 months ago #84839

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4281
  • Thank you received: 807
  • Karma: 370
tfj, I've modified for date format mm-dd-yyyy and tweaked the logic a bit in this code and attached survey.
<script>
 
	$(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 = (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>

File Attachment:

File Name: limesurvey_survey_96765.lss
File Size: 23 KB
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Calculate age in an Equation type question, using Date of Birth 2 years 2 months ago #84840

  • tfj
  • tfj's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 80
  • Thank you received: 6
  • Karma: 5
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
The administrator has disabled public write access.

Re: Calculate age in an Equation type question, using Date of Birth 2 years 2 months ago #84841

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4281
  • Thank you received: 807
  • Karma: 370
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Calculate age in an Equation type question, using Date of Birth 2 years 1 month ago #84933

  • tfj
  • tfj's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 80
  • Thank you received: 6
  • Karma: 5
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
The administrator has disabled public write access.

Re: Calculate age in an Equation type question, using Date of Birth 2 years 1 month ago #84934

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
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.
The administrator has disabled public write access.

Re: Calculate age in an Equation type question, using Date of Birth 2 years 1 month ago #85032

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4281
  • Thank you received: 807
  • Karma: 370
tfj, I don't have IE8 here to test but try changing:
var age = (today_year) - birth_year;

To:
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Calculate age in an Equation type question, using Date of Birth 2 years 1 month ago #85033

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4281
  • Thank you received: 807
  • Karma: 370
Date math is such a common need that we really ought to add date-related functions to Expression Manager...
Great idea Tom.
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Calculate age in an Equation type question, using Date of Birth 2 years 1 month ago #85043

  • tfj
  • tfj's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 80
  • Thank you received: 6
  • Karma: 5
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
The administrator has disabled public write access.

Re: Calculate age in an Equation type question, using Date of Birth 2 years 1 month ago #85210

  • tfj
  • tfj's Avatar
  • OFFLINE
  • Expert Lime
  • Posts: 80
  • Thank you received: 6
  • Karma: 5
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
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.150 seconds
Donation Image