Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Ho do I calculate "Today - DOB = age in years" in a PDF?

New Here ,
Jun 20, 2021 Jun 20, 2021

Copy link to clipboard

Copied

I have found a Script that caluates age into months however I need age in years. How do I change the below script?

 

var cDateFormat = "dd-mmm-yyyy";
var cDate = this.getField("dob").value;
if(cDate != "") {
var oDate = util.scand(cDateFormat, cDate);
}
if(oDate == null) app.alert("Invalid date: " + cDate, 1, 0);

else {
var now = new Date();
var nMonth = Number(now.getMonth());
var nYear = Number(now.getFullYear());
var diffYear = nYear - oDate.getFullYear();
var diffMonth = nMonth-oDate.getMonth();
if (diffYear) {
diffYear = diffYear * 12;
}

event.value = diffMonth + diffYear;
}

TOPICS
How to

Views

498

Likes

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

Adobe Community Professional , Jun 23, 2021 Jun 23, 2021
In closer inspection, the code you used is incorrect, so I fixed it for you. Try this: event.value = ""; var cDateFormat = "dd-mmm-yyyy"; var cDate = this.getField("dob").valueAsString; if (cDate != "") { var oDate = util.scand(cDateFormat, cDate); if (oDate == null) app.alert("Invalid date: " + cDate, 1, 0); else { var now = new Date(); var diffYear = now.getFullYear() - oDate.getFullYear(); var diffMonth = now.getMonth()-oDate.getMonth(); if (diffMonth<0) diffYear--; else if (di...

Likes

Translate

Translate
Adobe Community Professional ,
Jun 20, 2021 Jun 20, 2021

Copy link to clipboard

Copied

Which Adobe application are you using?

Likes

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
New Here ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

Hi John,

Im using Adobe Acrobat DC

Likes

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
Adobe Community Professional ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

Divide the result by 12 and round down.

Likes

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
New Here ,
Jun 22, 2021 Jun 22, 2021

Copy link to clipboard

Copied

Hi Try67,

 

Where do I insert this into the above script?

 

Likes

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
Adobe Community Professional ,
Jun 23, 2021 Jun 23, 2021

Copy link to clipboard

Copied

In closer inspection, the code you used is incorrect, so I fixed it for you. Try this:

 

event.value = "";
var cDateFormat = "dd-mmm-yyyy";
var cDate = this.getField("dob").valueAsString;
if (cDate != "") {
	var oDate = util.scand(cDateFormat, cDate);
	if (oDate == null) app.alert("Invalid date: " + cDate, 1, 0);
	else {
		var now = new Date();
		var diffYear = now.getFullYear() - oDate.getFullYear();
		var diffMonth = now.getMonth()-oDate.getMonth();
		if (diffMonth<0) diffYear--;
		else if (diffMonth==0) {
			var diffDay = now.getDate()-oDate.getDate();
			if (diffDay<0) diffYear--;
		}
		event.value = diffYear;
	}
}

Likes

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
New Here ,
Jun 23, 2021 Jun 23, 2021

Copy link to clipboard

Copied

LATEST

This works perfectly. Thank you try67!

Likes

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