1

I am having some difficulties getting a field to populate in an interactive PDF form. I am using a javascript to calculate the current age of client from 2 date fields (DateToday and ClientDOB) already in the form and I need it to populate a "ClientAge" field. The DateToday field automatically populates when the form is opened. I would like for the ClientAge field to populate after the user selects the ClientDOB.

This is what I am trying to have it do. Should be simple I would think.

DateToday - ClientDOB = ClientAge

Here is my code:

var DateToday_ = Date2Num(DateToday.formattedValue, "MM/DD/YYYY")
var ClientDOB_ = Date2Num(ClientDOB.formattedValue, "MM/DD/YYYY")
var diff = DateToday_ - ClientDOB_
ClientAge.value = Floor(diff / 365.25)

I am not sure why the ClientAge field will not populate once the ClientDOB has been selected. Any replies would be helpful. Thanks.

4

1 回答 1

0

这是从网上的某个地方拍摄的。不记得在哪里。但是,我以多种形式使用了它,并且效果很好。这个想法是日期之间的差异以毫秒为单位,给定日期是过去固定日期的秒数。一旦你知道日期之间的秒数差异(在这种情况下是 DOB 到现在),你就可以计算出那是多少年。请注意,我的格式是英国日期格式 (dd/mm/yy)。如果您以美国格式 (mm/dd/yy) 操作,则必须进行适当的更改。

// get current date THIS NON AMERCAN DATE FORMAT
var oNow = new Date();
// get date from 'Demo.DOB' field
var oMyDate = util.scand('dd/mm/yy', this.getField('Demo.DOB').value);
// define second in milliseconds
var nSec = 1000;
// define minute in milliseconds
var nMin = 60 * nSec;
// define hour in milliseconds
var nHr = 60 * nMin;
// define day in milliseconds
var nDay = 24 * nHr;
// compute today as number of days from epoch date
var nNowDays = Number(oNow) / nDay;
// truncate to whole days
nNowDays = Math.floor(nNowDays);
// compute inputted date days from epoch data
var nMyDateDays = Number(oMyDate) / nDay;
// truncate to whole days
nMyDateDays = Math.floor(nMyDateDays);
// compute difference in the number of days
var nDiffDays = nNowDays - nMyDateDays;
// adjust difference for counting starting day as 1
++nDiffDays;
// convert days to years
var nYears = nDiffDays / 365.2525
// truncate to whole years
nYears = Math.floor(nYears);
// set field value number of years (nYears)
event.value = nYears;
于 2014-09-05T03:25:18.087 回答