5 Replies Latest reply on Nov 30, 2012 2:41 PM by gkaiseril

    need to make a calculation run depending on if the date is before or after 10.25.12

    GOR1120 Level 1

      I have a field called Date of Loss.

      If this field is 10.25.12 or after then i need one calculation to run.

      if its before that date then i need a different one to run.

      I cannot find anything for this behavior.

        • 1. Re: need to make a calculation run depending on if the date is before or after 10.25.12
          gkaiseril MVP & Adobe Community Professional

          You have to write a custom calculation script and use the 'if then ... else ..." statement. Comparing dates can get tricky as you need to convert the date to a comparable value either using the JavaScript date object or breaking the date into year, month and date values and then creating the logical statement to identify the calculation to use. If you can put the date into the yyyymmdd format, ISO date format, you can easily make the comparison.

          • 2. Re: need to make a calculation run depending on if the date is before or after 10.25.12
            GOR1120 Level 1

            I have made the format of the Date of Loss field to the yyyymmdd format.

             

            I need a field called Service amount to = one of two fields depending on if the date is before or after 2012-10-25

             

            the field for dates before 2012-10-25 is called predateservamount

            the field for dates on or after 2012-10-25 is called postdateservamount

             

            I have a few ideas on ways to do this, but i'm not sure exactly how to put it together,

            would i do something like

            var date1 = new Date('2012-10-25');

            if (this.getField("Date of Loss").value=="<Date of Loss")

            event.value = this.getField("predateservamount").value;

             

            I am new at this so if That is way wrong please laugh all you need:)

            • 3. Re: need to make a calculation run depending on if the date is before or after 10.25.12
              gkaiseril MVP & Adobe Community Professional

              I would keep the field format as the users expect it.

               

              Then you can use JavaScript to reformat the date string as needed.

               

              For the field for the result of the calculation you could use the following Custom JavaScript calculation:

               

              // compare dates using ISO date format yyyymmdd

              // get date stirng values

              // field name for date input

              var cDateField = "Text1";

              try {

              var cDate1 = this.getField(cDateField).value;

              } catch (e) {

              app.alert("Could not get field named: " + cDateField, 0, 0);

              }

              event.value = "";

              if(cDate1 != "") {

              // continue only if date not empty

              var cDate2 = "25/OCT/2012"; // change as needed to match date format

              // convert to date object

              var cFormat = "mm/dd/yyyy";

              var oDate1 = util.scand(cFormat, cDate1);

              if(oDate1 == null) app.alert("Error in converting input date string: " + cFormat + " " + cDate1, 0, 0);

              var oDate2 = util.scand("mm/dd/yyyy", cDate2);

              if(oDate2 == null) app.alert("Error in converting cutoff date string: " + cFormat + " " + cDate2, 0, 0);

              // build ISO format date vlaues

              var date1 = util.printd("yyyymmdd", oDate1);

              var date2 = util.printd("yyyymmdd", oDate2);

              if(date1 < date2) {

              // date before Oct 25, 2012

              // add computation script or function

              event.value = "Before 10/25/2012";

              } else {

              // date on or after Oct 25, 2012

              // add computation script or function

              event.value = "Equal to or after 10/25/2012";

              } // end date compare

              } // end not empty

              • 4. Re: need to make a calculation run depending on if the date is before or after 10.25.12
                donnajean1234 Level 1

                I am having a problem with this formula, this is exactly what I need, I am trying to create a calculation that calculates mileage reimbursement.  The rate is dependent on the travel date.  I am not very familiar with this code, I attempted to use your code above and manipulate using my fields and values.  Any help would be GREATLY appreciated!  Here is the code I'm using.  I am getting an error on Line 2, column 5.

                 

                var ExpenseDate1 = "Text1";

                try {

                var cDate1 = this.getField(ExpenseDate1).value;

                } catch (e){

                1. app.alert("Could no get field named: " + ExpenseDate1,0,0);

                }

                1. event.value = "";

                if(cDate1!=""){

                var cDate2="01/Jan/2013";

                date format

                var cFormat = "mm/dd/yy";

                var oDate1 = util.scand(cFormat, cDate1);

                if(oDate1==null)app.alert("Error in converting input date string: " + cFormat + "" + cDate1,0,0);

                var oDate2 = util.scand("mm/dd/yyyy", cDate2);

                if(oDate2==null) app.alert("Error in converting cutoff date string: " + cFormat + "" + cDate2, 0, 0);

                var date1 = util.printd("yyyymmdd", oDate1);

                var date2 = util.printd("yyyymmdd", oDate2);

                if(date1 < date2) {

                Day1ExpAmt.value = (Day1ExpAmt * .555);

                } else {

                Day1ExpAmt.value = (Day1ExpAmt * .565);

                }

                }

                 

                 

                • 5. Re: need to make a calculation run depending on if the date is before or after 10.25.12
                  gkaiseril MVP & Adobe Community Professional

                  It looks like you are using a word processing program for editing you programming text. Program text does not use the "smart" tags and assumed formatting of word processors. If you must use a word processing program, then save as a "txt" type file and remove the smart quotes and other assumed formatting.