Skip to main content
CheDee317
Known Participant
August 19, 2018
Answered

How to assign the earliest date among 4 given dates?

  • August 19, 2018
  • 1 reply
  • 832 views

I have 4 solutions with different expiry dates and I need to assign the earliest expiry date among the 4 for the final product.  I am using Acrobat Pro and any help is greatly appreciated.  Thanks!

This topic has been closed for replies.
Correct answer try67

The dates array is for the values. If you want to hard-code the field names then use this:

var dateFormat = "mm/dd/yyyy";

var fields = ["801080EXP","801016EXP","801017EXP","801018EXP"];

var dates = [];

for (var i in fields) {

    var f = this.getField(fields);

    var v = f.valueAsString;

    if (v!="") dates.push(util.scand(dateFormat, v));

}

if (dates.length==0) event.value = "";

else {

    dates.sort(function(a, b){return a.getTime() - b.getTime()});

    event.value = util.printd(dateFormat, dates[0]);

}

1 reply

try67
Community Expert
Community Expert
August 19, 2018

Let's say the fields are named ExpiryDate1 to ExpireDate4, and that their date format is mm/dd/yyyy. You can use this code as the custom calculation script of the field where you want to display the earlier of the four values:

var dateFormat = "mm/dd/yyyy";

var dates = [];

for (var i=1; i<=4; i++) {

    var f = this.getField("ExpiryDate"+i);

    var v = f.valueAsString;

    if (v!="") dates.push(util.scand(dateFormat, v));

}

if (dates.length==0) event.value = "";

else {

    dates.sort(function(a, b){return a.getTime() - b.getTime()});

    event.value = util.printd(dateFormat, dates[0]);

}

CheDee317
CheDee317Author
Known Participant
August 19, 2018

Tried to substitute with what you've posted but couldn't get it right.  It's not showing any result.  I think I'm not doing it correctly.  Here's my script:

var dateFormat = "yyyy-mm-dd"; 

var dates = ["801080EXP","801016EXP","801017EXP","801018EXP"]; 

for (var i=1; i<=4; i++) { 

     var f = this.getField(["ExpDate"]+i); 

      var v = f.valueAsString; 

      if (v!="") dates.push(util.scand(dateFormat, v)); 

if (dates.length==0) event.value = ""; 

else { 

  1. dates.sort(function(a, b){return a.getTime() - b.getTime()}); 

event.value = util.printd(dateFormat, dates[0]); 

  }

the enclosed var dates are the field names for each component's expiry date and the finished product expiry date field name is "ExpDate".

Sorry for such a nuisance and thanks a lot for taking the time to reply!

CheDee317
CheDee317Author
Known Participant
August 19, 2018

The dates array is for the values. If you want to hard-code the field names then use this:

var dateFormat = "mm/dd/yyyy";

var fields = ["801080EXP","801016EXP","801017EXP","801018EXP"];

var dates = [];

for (var i in fields) {

    var f = this.getField(fields);

    var v = f.valueAsString;

    if (v!="") dates.push(util.scand(dateFormat, v));

}

if (dates.length==0) event.value = "";

else {

    dates.sort(function(a, b){return a.getTime() - b.getTime()});

    event.value = util.printd(dateFormat, dates[0]);

}


it worked!  a million thanks to you. !