1 Reply Latest reply on Dec 10, 2016 8:18 AM by ZoPaars

    Find the earliest date and highlight green and the latest date in red

    флорианб70160451 Level 1

      Dear all ,

       

      My form has 5 fields (field #1 -  field #5) with dates. Is there any javascript, with which I can identify the earliest date (automatically highlighted in green color and the latest date in red color?

       

      I would be greatfull for any ideas and comments.

       

      BR

      Florian

        • 1. Re: Find the earliest date and highlight green and the latest date in red
          ZoPaars Level 1

          I wrote a quick script that does what you want. I am not so experienced with JavaScript either, so there is probably a better way of doing this.

           

          This does assume that:

          1. Your field names are "field #1", "field #2", "field #3","field #4", "field #5"
          2. The dates in those fields are formated as "dd-mm-yyyy". If this is not true, adjust line 5 accordingly.

           

          //make an array of date values
          var dateArray = [[], []];
          for (var i = 1; i < 6; i++) {
              var Nm = "field #" + i, fld = this.getField(Nm);
              fld.textColor = color.black; //reset all the field colours
              var valDate = util.scand("dd-mm-yyyy", fld.value); //note that "dd-mm-yyyy" must correspond to how the dates are formatted in your fields
              if (fld.value && valDate) {
                  dateArray[0].push(valDate.valueOf());
                  dateArray[1].push(Nm);
              }
          }
          
          //get the max value
          var maxIndex = dateArray[0].indexOf(Math.max.apply(null, dateArray[0]));
          var maxFld = this.getField(dateArray[1][maxIndex]);
          
          //get the min value
          var minIndex = dateArray[0].indexOf(Math.min.apply(null, dateArray[0]));
          var minFld = this.getField(dateArray[1][minIndex]);
          
          //set the colours
          maxFld.textColor = color.green;
          minFld.textColor = color.red;
          

           

          You could add this script as the OnBlur event for each field. Or beter yet, add it as a document-level function and call that from each OnBlur event.