Skip to main content
Participating Frequently
February 11, 2019
Answered

Difference between two dates

  • February 11, 2019
  • 3 replies
  • 3147 views

I am using this script in the calculate field event: can anybody assist?

var dateFirst=StartDate1.formattedValue;

var dateSecond=Date1.formattedValue;

var timeDiff = Math.abs(dateSecond.getTime() - dateFirst.getTime());

var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));

Totaldays1=diffDays;

by all accounts this should work right?

This topic has been closed for replies.
Correct answer gkaiseril

You can use the following script to compute the difference in days. You may need to adjust the date string format and field names to match your form.

// start document level functions;

function GetField(oDoc, cField){

var oField = oDoc.getField(cField); // get field object for named field in specified PDF doc;

if(oField == null){

// trap and report error;

app.alert("Field " + cField + " not found.\nCheck for existance of field.", 1, 0, "Field Access Error");

}

return oField; // return field object;

}

function Scand(cFormat, cDateTime){

var oDate = util.scand(cFormat, cDateTime); // convert date string to date object using format;

if(oDate == null){

// trap and report conversion error;

app.alert("Error converting " + cDateTime + " using format " + cFormat, 1, 0, "Date/Time Conversion Error");

}

return oDate;

}

function Date2Days(cFormat, cDateTime){

var nDays = null;

var oDate = Scand(cFormat, cDateTime); // convert to date object;

if(oDate != null){

oDate.setHours(0, 0, 0, 0); // set to midnight;

nDays = Math.floor((oDate.getTime() / (1000 * 60 * 60 * 24))); // convert to whole days;

}

return nDays;

}

// end document level functions;

var nDiffDays = "";

var cDateFormat = "d-mmm-yyyy"; // change as needed to match date field format;

var oStart = GetField(this, "StartDate"); // field for start date;

var oEnd = GetField(this, "endDate"); // field for end date;

if(oStart != null && oStart.value != "" && oEnd != null && oEnd.value != ""){

// compute difference in days;

nDiffDays = Date2Days(cDateFormat, oEnd.value) - Date2Days(cDateFormat, oStart.value);

}

event.value = nDiffDays; // set field value;

The functions are used to minimize the amount of repeated code. The functions also include error reporting and trapping to help you in your debugging if needed. FormCalc includes some similar functions as built-in functions, but with Acrobat JavaScript one has to provide the code to do the job of the built-in functions.

3 replies

Inspiring
February 11, 2019

You code if using FormCalc and that script only works in forms created using LiveCycle.

gkaiserilCorrect answer
Inspiring
February 11, 2019

You can use the following script to compute the difference in days. You may need to adjust the date string format and field names to match your form.

// start document level functions;

function GetField(oDoc, cField){

var oField = oDoc.getField(cField); // get field object for named field in specified PDF doc;

if(oField == null){

// trap and report error;

app.alert("Field " + cField + " not found.\nCheck for existance of field.", 1, 0, "Field Access Error");

}

return oField; // return field object;

}

function Scand(cFormat, cDateTime){

var oDate = util.scand(cFormat, cDateTime); // convert date string to date object using format;

if(oDate == null){

// trap and report conversion error;

app.alert("Error converting " + cDateTime + " using format " + cFormat, 1, 0, "Date/Time Conversion Error");

}

return oDate;

}

function Date2Days(cFormat, cDateTime){

var nDays = null;

var oDate = Scand(cFormat, cDateTime); // convert to date object;

if(oDate != null){

oDate.setHours(0, 0, 0, 0); // set to midnight;

nDays = Math.floor((oDate.getTime() / (1000 * 60 * 60 * 24))); // convert to whole days;

}

return nDays;

}

// end document level functions;

var nDiffDays = "";

var cDateFormat = "d-mmm-yyyy"; // change as needed to match date field format;

var oStart = GetField(this, "StartDate"); // field for start date;

var oEnd = GetField(this, "endDate"); // field for end date;

if(oStart != null && oStart.value != "" && oEnd != null && oEnd.value != ""){

// compute difference in days;

nDiffDays = Date2Days(cDateFormat, oEnd.value) - Date2Days(cDateFormat, oStart.value);

}

event.value = nDiffDays; // set field value;

The functions are used to minimize the amount of repeated code. The functions also include error reporting and trapping to help you in your debugging if needed. FormCalc includes some similar functions as built-in functions, but with Acrobat JavaScript one has to provide the code to do the job of the built-in functions.

try67
Community Expert
Community Expert
February 11, 2019

Also, that's not how you access the value of a field, assuming this is an Acrobat form.

rodgerMaeAuthor
Participating Frequently
February 11, 2019

Ok, How do i access field values in acrobat DC? and would having to cast the field value to toString() work?

startDate1.toString();

Bernd Alheit
Community Expert
Community Expert
February 11, 2019

What are the field names?

Bernd Alheit
Community Expert
Community Expert
February 11, 2019

What is formattedValue? I have never seen this.