Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Difference between two dates

New Here ,
Feb 11, 2019 Feb 11, 2019

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?

TOPICS
Acrobat SDK and JavaScript , Windows
3.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 11, 2019 Feb 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;

}

func

...
Translate
Community Expert ,
Feb 11, 2019 Feb 11, 2019

What is formattedValue? I have never seen this.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2019 Feb 11, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 11, 2019 Feb 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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2019 Feb 11, 2019

What are the field names?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2019 Feb 11, 2019

If the field is called "StartDate1" then you access its value like this:

this.getField("StartDate1").valueAsString

Note that JavaScript is case-sensitive, so if the field is actually named "startDate1" the code above won't work.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2019 Feb 11, 2019

And to apply the result to the calculating field change the last line to:

event.value = diffDays;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 11, 2019 Feb 11, 2019

I have adjusted the code but still Not working:

var dateFirst=this.getField(StartDate1);

var dateSecond=this.getField(endDate1);

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

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

this.Totaldays1=diffDays;

StartDate1,endDate1 and Totaldays1 are the fields in the form....perhaps i am failing to assign the value i suppose

the rest are just varibale to hold dats for the calculation.:

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2019 Feb 11, 2019

You did not adjust it correctly. Study the example I gave carefully.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2019 Feb 11, 2019

Press ctrl+j and look for errors in the debugger.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 11, 2019 Feb 11, 2019

full code runs with no errors perhaps my assignment and accessing the fields might be the problem.

var dateFirst=this.getField("StartDate1").valueAsString;

var dateSecond=this.getField("endDate1").valueAsString;

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

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

this.Totaldays1=diffDays;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 11, 2019 Feb 11, 2019

Please read reply #6 again, as it tells you exactly what is wrong with your last line.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 11, 2019 Feb 11, 2019

var dateFirst=this.getField("StartDate1").valueAsString;

var dateSecond=this.getField("endDate1").valueAsString;

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

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

event.value = diffDays;

//please note i am doing this in properties calculated field tab in Totaldays1's field under custom calculation script maybe the approach is wrong

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2019 Feb 11, 2019

Read reply #12.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 11, 2019 Feb 11, 2019

Beside the last line, which you didn't change, there's another issue. You're treating the dateFirst and dateSecond variables as if they're Date objects. They're not. They just strings. You need to first convert them to Date objects (using util.scand) and then you'll be able to use the getTime method on them.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 11, 2019 Feb 11, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 11, 2019 Feb 11, 2019
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines