Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

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

Calculate age in an Equation type question, using Date of Birth 3 years 5 months ago #84792

  • tfj
  • tfj's Avatar
  • Offline
  • Expert Lime
  • Posts: 80
  • Thank you received: 6
  • Karma: 5
Greetings:

Has anyone calculated a person's age in a Equation type question, using the Date of Birth from a previous question?

I have been doing this with a MySQL query, but I would like to move it to LimeSurvey.

I am using 1.92+ build 120808 on a Windows test PC.

Thanks!

tfj
The administrator has disabled public write access.

Calculate age in an Equation type question, using Date of Birth 3 years 5 months ago #84806

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • Moderator Lime
  • Posts: 8218
  • Thank you received: 1122
  • Karma: 333
Hello,

There a lot of script in web to do this, www.google.com/search?q=javascript+birth+age+calculation

Need little adaptation for LS date.

Denis
The administrator has disabled public write access.

Calculate age in an Equation type question, using Date of Birth 3 years 5 months ago #84820

  • tfj
  • tfj's Avatar
  • Offline
  • Expert Lime
  • Posts: 80
  • Thank you received: 6
  • Karma: 5
Denis:

Thank you for responding.

Unfortunately for me, before asking my question on the Forum, I had already spent quite a bit of time on Google and a few hours attempting to use the "Implemented Functions" in Expression Manager (from the LimeSurvey documentation) to make this work in an Equation type question. I have found that if I struggle a bit before turning to the Forum, I usually learn things better.

I know I am taking the wrong approach in my efforts, so I am hoping someone who might have already done this could give me the "little adaptation for LS date" that you mentioned. If not, I'll keep at it until I figure it out (or until my deadline, at which point I'll stick with the MySQL query I had already written) and will share it on the Forum.

Thanks again!

tfj
The administrator has disabled public write access.

Calculate age in an Equation type question, using Date of Birth 3 years 5 months ago #84826

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • Moderator Lime
  • Posts: 8218
  • Thank you received: 1122
  • Karma: 333
Hello,

I don't know if EM can work.

But, with javascript:
First make a date with a date question, if you format is dd-mm-yy then:
Something like that to put the age in QQ:
<script type="text/javascript">
 function AgeFromBirth(birthdate,QQ) {
  aBirthDate=birthdate.split('-');
  if(aBirthDate.length == 3){
   birth = new Date(aBirthDate[2], aBirthDate[1] * 1 - 1, aBirthDate[0]);
   years = Math.floor((now.getTime() - born.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
  }
  $("#question"+QQ+" input.txt").val(years);
  }
$(document).ready(function() {
 AgeFromBirth({INSERTANS:sssXgggXqqq},212);
});
</script>
If date of birth is in the same page, the a
$("#questionQDATEOFBIRTHnumber input.date").blur(function(){AgeFromBirth($(this).val(),212);});
ca do the tric.

And put this function with workarounds help:
docs.limesurvey.org/tiki-index.php?page=..._etc._in_LimeSurvey_

Denis,
PS: there are surely some error in javascript code, i make it directly in the forum
PS2: calculation was taken here: pankajlalwani.wordpress.com/2010/08/02/a...ng-javascriptjquery/
The administrator has disabled public write access.

Calculate age in an Equation type question, using Date of Birth 3 years 5 months ago #84828

  • tfj
  • tfj's Avatar
  • Offline
  • Expert Lime
  • Posts: 80
  • Thank you received: 6
  • Karma: 5
Denis:

Thank you for taking the time out to help!

I'll work on the example you provided . . . at least I know now that I was going down the wrong path with Expression Manager . . .

Thanks again!

tfj
The administrator has disabled public write access.

Calculate age in an Equation type question, using Date of Birth 3 years 5 months ago #84831

  • Ben_V
  • Ben_V's Avatar
  • Offline
  • Platinum Lime
  • Posts: 1595
  • Thank you received: 391
  • Karma: 103
In this thread it seems that some LS users did it...
Ben/
Benoît
Last Edit: 3 years 5 months ago by Ben_V.
The administrator has disabled public write access.

Calculate age in an Equation type question, using Date of Birth 3 years 5 months ago #84832

  • tfj
  • tfj's Avatar
  • Offline
  • Expert Lime
  • Posts: 80
  • Thank you received: 6
  • Karma: 5
benitov:

Thanks for pointing me to this thread. This will also be helpful!

tfj
The administrator has disabled public write access.

Calculate age in an Equation type question, using Date of Birth 3 years 5 months ago #84833

  • Ben_V
  • Ben_V's Avatar
  • Offline
  • Platinum Lime
  • Posts: 1595
  • Thank you received: 391
  • Karma: 103
I've tried the 2nd code given on the thread using a date question type (with format dd.mm.yyyy) + a short-text question.... (both questions on the same page)
Everything is working fine with LS 1.90 but turned wrong with 1.92...
I don't know why...

File Attachment:

File Name: limesurvey..._164.lsg
File Size:8 KB
Benoît
The administrator has disabled public write access.

Calculate age in an Equation type question, using Date of Birth 3 years 5 months ago #84836

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5411
  • Thank you received: 1154
  • Karma: 479
Ben, it worked fine for me in 1.92. Maybe your IDs were wrong?

Here it is with more generic selectors to avoid the export-import/ID-change problem. The age value is simply loaded into the first short-text found.

File Attachment:

File Name: limesurvey...1324.lsg
File Size:10 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.

Calculate age in an Equation type question, using Date of Birth 3 years 5 months ago #84837

  • tfj
  • tfj's Avatar
  • Offline
  • Expert Lime
  • Posts: 80
  • Thank you received: 6
  • Karma: 5
Thanks, tpartner! I get a "NaN" error in the age text box.

On the off chance that it had something to do with the date format -- since I use "mm-dd-yyyy" -- I changed this setting to "dd-mm-yyyy" for the survey and I still got the "NaN" error. In the meantime, I changed it back to "mm-dd-yyyy."

This is another of those times where I figure I am missing something simple.

tfj
The administrator has disabled public write access.

Calculate age in an Equation type question, using Date of Birth 3 years 5 months ago #84838

  • Ben_V
  • Ben_V's Avatar
  • Offline
  • Platinum Lime
  • Posts: 1595
  • Thank you received: 391
  • Karma: 103
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
The administrator has disabled public write access.

Calculate age in an Equation type question, using Date of Birth 3 years 5 months ago #84839

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5411
  • Thank you received: 1154
  • Karma: 479
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...6765.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.

Calculate age in an Equation type question, using Date of Birth 3 years 5 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.

Calculate age in an Equation type question, using Date of Birth 3 years 5 months ago #84841

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5411
  • Thank you received: 1154
  • Karma: 479
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.

Calculate age in an Equation type question, using Date of Birth 3 years 5 months 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.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.194 seconds