• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
7

Age calculation

Community Beginner ,
Jan 16, 2024 Jan 16, 2024

Copy link to clipboard

Copied

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?

TOPICS
How to , JavaScript , PDF forms

Views

201

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 16, 2024 Jan 16, 2024

Votes

Translate

Translate
Community Expert ,
Jan 16, 2024 Jan 16, 2024

Copy link to clipboard

Copied

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;}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 19, 2024 Jan 19, 2024

Copy link to clipboard

Copied

This returns NaN in the age field.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 19, 2024 Jan 19, 2024

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 16, 2024 Jan 16, 2024

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines