Expand my Community achievements bar.

SOLVED

Script to calculate age from first 6 digits of a 10 digit number (DDMMYYXXXX)

Avatar

Level 3

I have a  "numeric field" which will contain a 10 digit number; the fist 4 digits being the date of birth (DDMMYY). 

I would really like to have another field where the DOB is automatically calculated from this. I'd appreciated any help - I have pretty much zero knowledge of scripting.

1 Accepted Solution

Avatar

Correct answer by
Employee

ok, I think I can see the problem. CHI is a numeric field and not a text field. Therefore the CHId is not a string but a digital number - that means that "0101191234" is actually a numeric value of 101191234. That cannot be dismantled with substring.

Change the CHI field to textfield and then try again.I tested your fields with the correct type and then it works:

kprokopi_0-1598614241180.pngkprokopi_1-1598614314930.png

To force format and length of CHI limit max characters

kprokopi_0-1598619725694.png

Display and validation patterns text{9999999999} and error on validation

kprokopi_1-1598619785968.pngkprokopi_2-1598619812681.png

 

 

 

View solution in original post

22 Replies

Avatar

Employee

here is one very simple approach using strings and substrings:

kprokopi_0-1598426262380.png

In the calculate event of the second text field I have the following javascript code:

 

var id = TextField1.rawValue; //10 digit id  from 1st field
id.substring(0,2)+"-"+id.substring(2,4)+"-"+id.substring(4,6)

 

The substring extract the day, month and year - how you combine them in the date string is up to you.

Avatar

Employee
sorry I misread - you wanted age - I will look at that too

Avatar

Level 3
Hi, yes I am looking for age - would be very grateful if you could help with that! Also, if you could tell me where to add the script I need that would be helpful too.

Avatar

Employee

ok, age calculation

kprokopi_0-1598429122390.png

TextField 2 is set in calculate event like that

var id = TextField1.rawValue;
"19" + id.substring(4,6)+"-"+id.substring(2,4)+"-"+id.substring(0,2)+"T00:00:00"

BUT your approach has a weakness - 2 digit years cannot be interpreted correctly 20 can be 1920 and 2020 - you may need either 4 digits or come up with some rule how to detect the century. Javascript uses 120 for 2020 and 20 for 1920.


Calculate in Age Field:

var now = new Date();
var today = new Date(now.getYear(),now.getMonth(),now.getDate());
var birthDateString = TextField2.rawValue;
var birthDate = new Date(birthDateString);

// calculate years
var years = (today.getFullYear() - birthDate.getYear());
// adjust years depending on months
if (today.getMonth() < birthDate.getMonth() ||
today.getMonth() == birthDate.getMonth() && today.getDate() < birthDate.getDate()) {
years--;
}

years;

 

 

Avatar

Level 10

Here's what I would do.

Put it to the exit event of your numeric field.

 

function calculateAge(aDate) {
	var oDateOfBirth = new Date(aDate[0], aDate[1] - 1, aDate[2]),
		iDiff, oAge,
		oDate = new Date();	// get current date
	oDate.setHours(0, 0, 0); // reset hours, minutes and seconds
	oDate.setMilliseconds(0); // reset milliseconds
    iDiff = parseInt(oDate.valueOf(), 10) - oDateOfBirth.getTime() - 1,
    dAge = new Date(iDiff);
    return Math.abs(dAge.getUTCFullYear() - 1970);
}

var	cValue = this.rawValue,	
	aDate = [cValue.substring(4,6), cValue.substring(2,4), cValue.substring(0,2)],
	nAge = calculateAge(aDate);
Textfield1.rawValue = nAge; // show age in Textfield1

Avatar

Level 3
Thanks radzmar. I selected the numeric field, and pasted the code above into the script editor ("show" says "calculate"). Do I need to change "Textfield1" in your script to the name of the field that will contain the age? If so, do I just put the name of the field, or do I have to enter all the subforms also (both fields are in the same subform)? Thanks

Avatar

Level 10
Yes, you need to replace Textfield1 with the SOM expression of the field you want to display the age in your form.

Avatar

Level 3

Thanks radzmar. I replaced "Textfield1" with "form1.Page1subform.PatientSubform.AgeCalculated" in your script, but no age is outputted. Just to confirm, the first field is a numeric field, and the output field is a text field with value type "user entered - optional"?

Avatar

Level 10
Make sure you set the language in the script editor to JavaScript, not FormCalc. If the output field is a text field it should work. However, I can't say if you SOM expression is correct. I would have to see at least the forms hierachy to check this.

Avatar

Level 3
It's set to JavaScript, and I'm pretty sure I have the correct SOM expression - but it's still not working. Do I need to set any particular settings in the numeric field's field or value settings?

Avatar

Employee
A few remarks if it still does not work: @radzmar said that you should place this in the EXIT event NOT the calculate event as you wrote in your first comment. This script must be in the date input field and NOT the numeric field as in my example because this is a different approach.

Avatar

Level 10
Just make a screenshot of your forms hierarchy, so we can see where the fields are located.

Avatar

Level 3
kprokopi - well observed; somehow I had placed it under "calculate" - I changed it to "exit*" - though unfortunately it still doesn't work. You say that "the script must be in the date input field" - I presume you mean the numeric field? The 10-digit number is being entered into a numeric field. That is where I entered the script. I will post a screenshot of the script & hierarchy

Avatar

Level 10
AgeCalculated.rawValue = nAge; should already work, since both fields are in the same sub tree of the hierachy- Make sure that the numeric field is not updated by any other calculation script.

Avatar

Level 3

You're right radzmar, they're both in the same subtree. I think I just found the problem - there was a validation pattern set to the CHI field - when I removed this, it works! But now the user can enter the wrong number of digits. I do need a validation pattern to make sure that the user enters exactly 10 digits.

Avatar

Level 10
Open the file in Acrobat and check the JavaScript console (Ctrl + J) for any errors.

Avatar

Correct answer by
Employee

ok, I think I can see the problem. CHI is a numeric field and not a text field. Therefore the CHId is not a string but a digital number - that means that "0101191234" is actually a numeric value of 101191234. That cannot be dismantled with substring.

Change the CHI field to textfield and then try again.I tested your fields with the correct type and then it works:

kprokopi_0-1598614241180.pngkprokopi_1-1598614314930.png

To force format and length of CHI limit max characters

kprokopi_0-1598619725694.png

Display and validation patterns text{9999999999} and error on validation

kprokopi_1-1598619785968.pngkprokopi_2-1598619812681.png

 

 

 

Avatar

Level 3

CHI is actually a text field already! I'm not sure why I said it was a numeric field, sorry! I actually got it working on the advice of radzmar, though I still need to set a validation pattern to make sure that the user enters a 10 digit number.