Skip to main content
Participating Frequently
January 16, 2024
Answered

Age calculation

  • January 16, 2024
  • 3 replies
  • 1366 views

HI! I am trying to calculate mother's age in years on baby's birth date. I have a field to enter the mother's birthdate (BM DOB) and a field to enter the baby's birth date (Acq Date). Is this possible to auto-calculate in a read-only text box?

This topic has been closed for replies.
Correct answer JR Boulay

See this great sample file: https://abracadabrapdf.net/file/Age_Capitaine.pdf

3 replies

Participant
July 1, 2024

Yes, it's possible to auto-calculate the mother's age on the baby's birth date using a script or formula in the read-only text box. Consider using an Age calculator because this feature saves time, ensures accuracy, and simplifies record-keeping.

JR Boulay
Community Expert
JR BoulayCommunity ExpertCorrect answer
Community Expert
January 16, 2024
Acrobate du PDF, InDesigner et Photoshopographe
Nesa Nurani
Community Expert
Community Expert
January 16, 2024

Try this as custom calculation script of 'age' field:

var date1 = this.getField("BM DOB").valueAsString;
var date2 = this.getField("Acq Date").valueAsString;

if (date1 === "" || date2 === "")
 event.value = "";
else {
 var mdob = new Date(date1);
 var bdob = new Date(date2);
 var age = bdob.getFullYear() - mdob.getFullYear();

 if (bdob.getMonth() < mdob.getMonth() ||
   (bdob.getMonth() === mdob.getMonth() && bdob.getDate() < mdob.getDate()))
    age--;
 event.value = age;}
Participating Frequently
January 19, 2024

This returns NaN in the age field.

JR Boulay
Community Expert
Community Expert
January 19, 2024

Don't use any Format in the Age field, and set it to read only.

Acrobate du PDF, InDesigner et Photoshopographe