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

How to assign the earliest date among 4 given dates?

Community Beginner ,
Aug 18, 2018 Aug 18, 2018

Copy link to clipboard

Copied

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!

TOPICS
Acrobat SDK and JavaScript , Windows

Views

471

Translate

Translate

Report

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

Community Expert , Aug 19, 2018 Aug 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]);

}

Votes

Translate

Translate
Community Expert ,
Aug 19, 2018 Aug 19, 2018

Copy link to clipboard

Copied

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]);

}

Votes

Translate

Translate

Report

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 Beginner ,
Aug 19, 2018 Aug 19, 2018

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

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 ,
Aug 19, 2018 Aug 19, 2018

Copy link to clipboard

Copied

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]);

}

Votes

Translate

Translate

Report

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 Beginner ,
Aug 19, 2018 Aug 19, 2018

Copy link to clipboard

Copied

LATEST

it worked!  a million thanks to you. !

Votes

Translate

Translate

Report

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