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

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

1.2K

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

Votes

Translate

Translate
Community Expert ,
Jun 20, 2021 Jun 20, 2021

Copy link to clipboard

Copied

Which Adobe application are you using?

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

Copy link to clipboard

Copied

Hi John,

Im using Adobe Acrobat DC

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

Copy link to clipboard

Copied

Divide the result by 12 and round down.

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

Copy link to clipboard

Copied

Hi Try67,

 

Where do I insert this into the above script?

 

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

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

Copy link to clipboard

Copied

LATEST

This works perfectly. Thank you try67!

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