Skip to main content
New Participant
December 30, 2024
Question

Calculating Difference of Dates in Days

  • December 30, 2024
  • 2 replies
  • 763 views

Hello, 

 

I would like to inquire for help. I have built a fillable form in Adobe Acrobat Pro and am trying to calculate the total days left to complete a project. In simplest notation, the formula would read ((ProjectEndDate1)-(TodaysDate))=(DaysLeft1).

 

This would be done similarly across 10 different projects which share the same form, so the indicator number would change per project, (i.e., 1, 2, 3,....10.).

 

Other Info:  Both fields, (ProjectEndDate1) and (TodaysDate) and standard drop down date fields, where as (DaysLeft1) is a text box formatted as a number, is this correct?

 

I am unfamiliar with Java Script and its Syntax; would you be able to write a formula for me that would calculate this? Is JavaScript needed or can this formula be calculated in a simpler way?

This topic has been closed for replies.

2 replies

Thom Parker
Community Expert
Community Expert
December 30, 2024

Here's some articles on scripting dates and times.

https://www.pdfscripting.com/public/Date-and-Time-Handling.cfm

https://acrobatusers.com/tutorials/date_time_part1/

https://acrobatusers.com/tutorials/date_time_part2/

 

Date and time calculations are handled differently from regular number calculations because they are not represented as simple numbers. That said, a data/time can be converted to a number. So the issue is always with converting between the date/time format and the number. You need to know the date format first, then the Acrobat JS "util.scand" function can be used to parse the date/time and covert it to a Date object, then the date object is used to calculate the difference.  Like this.

 

 

var strDate1 = this.getfield("Date1").valueAsString;
var strDate2 = this.getfield("Date2").valueAsString;
var oDate1 = util.scand("...Format..", strDate1);
var oDate2 = util.scand("...Format..", strDate2);

// The date difference will be the number of milliseconds between the dates. 
var nDiff = oDate1 - oDate2;

var nDays = nDiff/(1000*60*60*24);

this.getField("Result").value = nDays;



 

 

 

So you need to fill in the date format and the correct field names. The script is also missing a test to validate the dates field values before doing the conversion to the date objects. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
PDF Automation Station
Community Expert
Community Expert
December 30, 2024

Assuming a date format of mm/dd/yyyy, enter the following custom calculation script into the DaysLeft field:

 

var endDate=this.getField("ProjectEndDate1").value;
var today=this.getField("TodaysDate").value;
if(!endDate || !today)
{event.value=""}
else
{
endDate=util.scand("mm/dd/yyyy", endDate);
today=util.scand("mm/dd/yyyy", today);
event.value=Math.floor((endDate-today)/86400000)+1;
}