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;
}
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
...
Copy link to clipboard
Copied
Which Adobe application are you using?
Copy link to clipboard
Copied
Hi John,
Im using Adobe Acrobat DC
Copy link to clipboard
Copied
Divide the result by 12 and round down.
Copy link to clipboard
Copied
Hi Try67,
Where do I insert this into the above script?
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;
}
}
Copy link to clipboard
Copied
This works perfectly. Thank you try67!