2 Replies Latest reply on Sep 28, 2012 12:44 PM by gkaiseril

    need help creating a formula to calculate the number of days between two dates.

    antoniocarlosnogueiralima Level 1

      need help creating a formula to calculate the number of days between two dates.

        • 1. Re: need help creating a formula to calculate the number of days between two dates.
          George_Johnson MVP & Adobe Community Professional

          The two dates would be obtained from form field entries, correct? How will the date fields be formatted (e.g., mm/dd/yy, yyyy/mm/dd, or something else)?

          • 2. Re: need help creating a formula to calculate the number of days between two dates.
            gkaiseril MVP & Adobe Community Professional

            You need to use the Custom JavaScript calculation and will need to know the form field names and formats of the dates.

             

            function Date2Num(cFormat, cDate) {
            /* convert the date string cDate to the numberof days
            from the JavaScript Epoch date.
            inputs: cFormat - format of the date string
            cDate - character string of the date value

            returns the number of days since the JS Epoch date
            */
            // convert the date string to a JS date object
            var oDate = util.scand(cFormat, cDate);
            if(oDate != null) {
            // no conversion error
            // get the milliseonds from the date object
            var nMsecs = Number(oDate);
            // convert to days
            nDays = nMsecs / (1000 * 60 * 60 * 24);
            // truncate to whole days
            nDays = Math.floor(nDays);
            } else {
            // conversion error
            app.alert("Conversion error date: " + cDate + " with format string: " +cFormat);
            nDays = "";
            }
            return nDays;
            } // end Date2Num function


            // field names and formats for the input dates
            // starting date
            var cStartDate = "StartingDate"; // name of field
            var cStartFormat = "dd-mmm-yyyy"; // date format of field
            // ending date
            var cEndDate = "StartingDate"; // name of field
            var cEndFormat = "dd-mmm-yyyy"; // date format of field
            // get the values from the fields
            var cStart = this.getField(cStartDate).valueAsString;
            var cEnd = this.getField(cEndDate).valueAsString;
            event.value = 0: // default value
            // process only if we have both dates
            if(cStart != "" && cEnd != "") {
            event.value = Date2Num(cEndFormat, cEnd) - Date2Num(cStartFormat, cStart);
            }

             

            Change the field names and formats as necessary.