Quantcast
Channel: Adobe Community: Message List
Viewing all articles
Browse latest Browse all 82585

Re: Javascript Subtracting Dates

$
0
0

You have to use the Acrobat JavaScript "util.scand" method to create the date object for each date and then use the "getTime()" method to get the number of milliseconds since the Epoch Date, midnight January 1, 1970. You can then do the math to get the difference of the two values and convert the result to days. Then it is a simple "if" statement to set the text field.

 

 

// doucment level scripts;
function GetField(cName) {
// get field object for field named cName;
var oField = this.getField(cName); // get field object;
if(oField == null) {
  // null value is error condition;
  app.alert("Error getting field named \"" + cName + "\"!", 0, 0);
} // end error trapping and reporting;
return oField;
} // end GetField function;

function Scand(cFormat, cString) {
// util.scand with error chedking;
var oDate = util.scand(cFormat, cString);
if(oDate == null) app.alertr("Error converting date value " + cString + " with format image of " + cFormat, 0, 1);
return oDate
} // end Scand function;

function Date2Days(cFormat, cString) {
var oDate = Scand(cFormat, cString);
if(oDate == null) app.alertr("Error converting date value " + cString + " with format image of " + cFormat, 0, 1);
var nDate = oDate.getTime();
return Math.floor(nDate / (1000 * 60 * 60 * 24));
}
// end document level scripts;

 

 

// custom JavaScript calculation to compute difference in days between 2 dates;

// field names;

var cStartField = "EffectiveDate";

var cEndField = "TerminationDate";

// date format image;

var cDateFormat = "mm/dd/yyyy";

 

function FunctionExist(cName) {
// see if a function exist;
var bExist = eval("typeof " + cName + " == \"function\"");
if(!bExist) app.alert("Missing needed function\" " + cName + "\"!\nPlease install the necessary function.", 1, 0);
return bExist;
} // end FunctionExist function;


// test for user written functions that are needed for this script;
FunctionExist("GetField");
FunctionExist("Scand");
FunctionExist("Date2Days");

 

event.value = "";
// get field objects;
var oStart = GetField(cStartField);
var oEnd = GetField(cEndField);
if(oStart.valueAsString != "" && oEnd.valueAsString != "") {
event.value = "No";
var nStart = Date2Days(cDateFormat, oStart.valueAsString);
var nEnd = Date2Days(cDateFormat, oEnd.valueAsString);
var nDiff = nEnd - nStart;
// test for difference greater than 90;
if(nDiff > 90) {
  event.value = "Yes";
}
/*
// debugging informattion;
console.show();
console.clear();
conole.println("Start: " + oStart.valueAsString);
console.println("End: " + oEnd.valueAsString);
console.println("Start days: " + nStart);
console.println("End days: " + nEnd);
console.println(nEnd - nStart);
console.println("Days: " + nDiff);
// end debuggig information;
*/
}
// end custom calculaion script;


Viewing all articles
Browse latest Browse all 82585

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>