In Date and Time in PDFs, Part 1 I described a couple of different ways of creating a date object and how to display the date object with a specific date format. Date objects can be used to calculate past and future dates relative to the passage of a specific amount of time, or the amount of time between two dates.
Number of Days Between Two Dates
When .getTime( ) is added to a date object it returns the number of milliseconds since, or prior to, the start of Jan 1, 1970. To calculate the number of days between two dates, subtract the passage of milliseconds for the start date from the passage of milliseconds for the end date, and convert the difference to days by dividing by the number of milliseconds in a day. You can determine the number of milliseconds in a day like this:
number of milliseconds in a second (1000) x number of seconds in a minute (60) x number of minutes in an hour (60) x number of hours in a day (24) = 86,400,000
You can test the result by running the following script in the console:
1000*60*60*24
In theory, when you create a date object using a specific date and run that date object with the getTime( ) method, it should return the exact same number of milliseconds every time. This is not the case in reality but the results will usually vary be less than 1,000 milliseconds, or one second. Dividing the difference of milliseconds passed for two dates by 86,400,000 can result in an extremely large number of decimal places so the quotient should be rounded down to a whole number using Math.floor( ). Suppose you have two fields in a PDF form named "Start Date" and "End Date", both formatted as date fields with the mm/dd/yyyy format, and you also have a field called "Days", in which you want to calculate the number of days between the dates entered into the two date fields. Use the following custom calculation in the Days field:
//define a variable for the start date field value
var sStart = this.getField("StartDate").valueAsString;
//define a variable for the end date field variable
var sEnd = this.getField("EndDate").valueAsString;
if(sStart && sEnd)//test that both fields have values
{
//if both fields have values execute the following script:
//define a variable for the number of milliseconds passed until start date
var dStart = util.scand("m/d/yyyy", sStart).getTime();
//define a variable for the number of milliseconds passed until end date
var dEnd = util.scand("m/d/yyyy", sEnd).getTime();
//divide the number of milliseconds in a day by the difference and and round down
event.value = Math.floor((dEnd - dStart) / 86400000);
}
else
{
//if either field does not contain a value, don't run the calculation
event.value = "";
}
Take the Acrobat Pro/JavaScript eLearning Course Acrobat Like a Pro
What is the Date When You Add a Number of Days to a Date?
In Part 1 of Date and Time in PDFs, I described how to use the milliseconds from Jan 1, 1970, or the getTime( ) method of a date object as the input parameter for new Date( ) to get the date object for a specific date. To display a date that is x number of days from another date you would simply add the milliseconds to the original and display the new date using util.printd and the new date object. Here’s an example of how to display the date that is five days from today:
var days=5;
var futureDate=new Date().getTime() + days*86400000;
util.printd("mm/dd/yyyy", new Date(futureDate));
Change the 5 in first line to the number of days from today that you require. There’s another way to do this using getDate( ) and setDate( ) methods:
var days=5;
var date= new Date();
date.setDate(date.getDate()+days);
util.printd("mm/dd/yyyy",date);
Again, you can change the 5 to the required number. You can also work backwards to get a previous date in both examples by making the days variable negative:
//100 days ago
var days=-100;
var date= new Date();
date.setDate(date.getDate()+days);
util.printd("mm/dd/yyyy",date);
How To Calculate Someone’s Age Today
The getFullYear( ) method of the date object will return the 4-digit year of the date and the getMonth( ) method will return the month of the date object. Calculating an age is a little trickier, however, than simply subtracting the year of birth from the current year because the result depends on whether the birthday has passed this year. The trick is to start with subtracting the year of birth from the current year, then subtracting 1 from the difference. Then test whether the month of birth has passed this year. If so, add one year. If we are currently in the month of birth, then test whether the day of birth has passed - or whether it is today. If so, add one year. Assume the PDF has a date field named "DOB" that is formatted mm/dd/yyyy. Assume an age field which contains the following custom calculation script:
if (this.getField("DOB").value!="")
{
var dob = util.scand("mm/dd/yyyy", this.getField("DOB").valueAsString);
var today = new Date();
var age = today.getFullYear() - dob.getFullYear() - 1;
if (today.getMonth()>dob.getMonth())
{age++;}
else if (today.getMonth()==dob.getMonth())
{
if (today.getDate()>=dob.getDate())
{age++;}
}
event.value = age;
}