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

lostfrogg

25-08-2020

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.

Accepted Solutions (1)

Accepted Solutions (1)

Kosta_Prokopiu1

Employee

28-08-2020

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

 

 

 

Answers (3)

Answers (3)

Kosta_Prokopiu1

Employee

26-08-2020

🙂 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.

radzmar

MVP

26-08-2020

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
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
Yes, you need to replace Textfield1 with the SOM expression of the field you want to display the age in your form.

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"?

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.
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?
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.
Just make a screenshot of your forms hierarchy, so we can see where the fields are located.
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
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.

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.

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

Kosta_Prokopiu1

Employee

26-08-2020

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;