Age field based on date field


how create field populates age based on date field?

 

adobe acrobat 9

you have use javascript create custom calculation script desired result.

 

 

for example in decimal years:

 

function date2num(cformat, cdate) {
/*
convert date string number of days since epoch date
inputs:
cdate - date sting
cformat - format of date sting
returns:
number of dasy since epoc date.
*/
// convert date string date object
var odate = util.scand(cformat, cdate);
// convert milliseconds epoch date whole days
return math.floor(odate.gettime() / (1000 * 60 * 60 * 24));
} // end of date2num

event.value = "";
// dob string value
var sdob = this.getfield("dob").value;
if(sdob != "") {
// compute dob not empty
var ctoday = util.printd( "dd-mmm-yyyy", (new date()))
// compute difference includig end date
var ndiff = date2num(ctoday, "dd-mmm-yyyy", ctoday) - date2num("dd-mmm-yyyy", sdob);
// compute output in years decimals
event.value =(ndiff / 365.2425) + " years old";
}

 

 

years months & days:

 

event.value = "";

// dob string value

var sdob = this.getfield("dob").value;

if(sdob != "") {

// convert date object

var odob = util.scand("dd-mmm-yyyy", sdob);

// todays date in milliseconds since epoch date

var otoday = new date()

// compute output

nyears = otoday.getfullyear() - odob.getfullyear();

nmonths = otoday.getmonth() - odob.getmonth();

ndays = otoday.getdate() - odob.getdate();

if(ndays < 0) {

// table months days

// fix days in month

nmonths--;

ndays = ndays + 30;

}

if(nmonths < 0) {

nyears--;

nmonths = nmonths + 12;

}

event.value = nyears + " years " + nmonths + "  months " + ndays + " days";

}

 

whole years only:

 

event.value = "";

var dobvalue = getfield("dob").value;

if (dobvalue!="") {

var dob = util.scand("dd/mm/yyyy", dobvalue);

var today = new date();

// compute years on full year only

var age = today.getfullyear() - dob.getfullyear();

var todaymmdd = today.getmonth() + today.getdate()

var dobmmdd = dob.getmonth() + dob.getdate();

if( todaymmdd < dobmmdd) age -= 1;

event.value = age;

}



More discussions in PDF Forms


adobe

Comments

Popular posts from this blog

how to devide a circle into equal parts

"Could not fill because there are not enough opaque source pixels" - not solved by any other thread

Why can't I change the billing info for my account?