5 Replies Latest reply on Jan 23, 2014 5:51 AM by mlstechwriter71

    Calculating Previous Month(s) From Current Date

    mlstechwriter71 Level 1

      Greeting JavaScripting Gurus!

       

      I would like to ask for some assistance on Date Calculation.

      Here is my current scenario:

       

      The user inputs a date mm/dd/yyyy ("DateField")

      The powers that be over here are asking for the "DateField" to then calculate these items:

       

      "DateField" minus(-) 12 months = mm/yyyy ("12MonthsAgoDateField")

      "DateField" minus(-) 18months = mm/yyyy ("18MonthsAgoDateField")

      "DateField" minus(-) 36 months = mm/yyyy ("36MonthsAgoDateField")

      and so on...

       

      I've read some of the Date Calculation posts, but could no seem to find anything that fit this scenario.

      I freely confess my javascripting ability is very minor.

      I would greatly appreciate any help in scripting this.

       

      Thank you!

        • 1. Re: Calculating Previous Month(s) From Current Date
          James A Hiltz CET Level 1

          The following is the script I used to perform a simmaler task.

           

           

          // Get date from field

          var v = getField("add your reference date field name here").value;

           

          // Get the field values, as numbers

          var d1 = getField("add your field name").value;

           

          if (v && d1) {

           

              // Convert string to date

              var d = util.scand('mmmm/dd/yyyy', v);

           

              // add d1 as days

          d.setDate(d.getDate() + d1);

           

              // Set this field value to the new date

              event.value = util.printd("mm/dd/yyyy", d);

            

          } else {

           

              // Clear this field

              event.value = "";

          }

          1 person found this helpful
          • 2. Re: Calculating Previous Month(s) From Current Date
            gkaiseril MVP & Adobe Community Professional

            Unfortunately the numbers of days in a month or year are not the same for all months or years, so the get and set date methods might not work. Fortunately there are the getMonth() and setFullYear() methods for getting or setting the month or year for a given date.

             

            For the "On Blur" action for the "DateField" you can use:

             

            function GetField(cName) {
            // get a field object with error catching;
            var oField = this.getField(cName);
            if(oField ==  null) app.alert("Error accessing field named: " + cName, 0, 0);
            return oField;
            } // end GetField function;

             

            function AddMonths(oDate, nMonths) {
            // add nMonths to oDate object;
            oDate.setMonth(oDate.getMonth() + nMonths);
            return oDate; // return adjusted date object;
            } // end AddMonths;

             

            function Scand(cFormat, cDate) {
            var oDate = util.scand(cDateFormat, event.value);
            if(oDate == null) app.alert("Error converting " + oDate.valueAsString + " with format: " + cDateFormat, 0, 0);
            return oDate;
            } // end Scand functon;

             

            var cDateFormat = "mm/dd/yyyy"; // format for date strings;
            // event value is the start date string;
            // 12 months ago;
            var o12MonthsAgo = GetField("12MothsAgoDateField")
            var oDate = Scand(cDateFormat, event.value);
            oDate = AddMonths(oDate, -12) // subtract 12 months;
            o12MonthsAgo.value = util.printd(cDateFormat,oDate);
            // 18 months ago;
            var o18MonthsAgo = GetField("18MothsAgoDateField")
            oDate = Scand(cDateFormat, event.value);
            oDate = AddMonths(oDate, -18) // subtract 18 months;
            o18MonthsAgo.value = util.printd(cDateFormat, oDate);
            // 36 months ago;
            var o36MonthsAgo = GetField("36MothsAgoDateField")
            oDate = Scand(cDateFormat, event.value);
            oDate = AddMonths(oDate, -36); // subtract 36 months;
            o36MonthsAgo.value = util.printd(cDateFormat, oDate);
            // and so on;


            Since getting a field object, converting a date string to a date object and adjusting the date object using the getMonth and setMonth methods are repeated several times I have used functions so the repeated code could be reused.

            1 person found this helpful
            • 3. Re: Calculating Previous Month(s) From Current Date
              mlstechwriter71 Level 1

              Wow... Thank you! There is no way I could have figured all of this out!

              As I have confessed - I don't do alot of scripting, so please excuse the next bit of questions.

               

              Is it possible to report just the Month/Year for the various "##MonthsAgoDateFields"?

               

              Also I set the the Action for "Mouse Up" instead of "On Blur", since that fits the requirements for this document better - and this is working but it is showing the complete date mm/dd/yyyy.

               

              Thank you again for all the help!

              • 4. Re: Calculating Previous Month(s) From Current Date
                Mohammad Irfan

                If you really don't want to go in details (or not have time to do that)

                Here is the simple solution of your problem.

                 

                I have written a small function (a few lines)

                 

                Put this in document level javascript

                 

                // Document level javascript function -----

                 

                function getPastDate(textFieldName, months){

                var cDate = util.printd("mm/dd/yyyy",new Date(this.getField(textFieldName).value));

                var sDate = util.scand("mm/dd/yyyy", cDate);

                          if ( sDate== null ){

                                    app.alert("Please enter a valid date of the form \"mm/dd/yyyy\".")

                          }else {

                          var pDate = new Date(sDate.getFullYear(),(sDate.getMonth() + months),sDate.getDate());

                          return util.printd("mm/yyyy",pDate);

                          }

                }

                 

                 

                Now suppose we have a textbox name "Text1" having date with format "mm/dd/yyyy"

                1st parameter is for textbox name (having the date you want to process)

                2nd parameter is number of months (to subtract pass a negative value)

                 

                If you want to get result in another textbox "Text2" write the code below in the calculate event of Text2

                 

                event.value = getPastDate("Text1",-12);

                 

                // This will subtract 12 months from "Text1" value

                 

                Hope this will help

                • 5. Re: Calculating Previous Month(s) From Current Date
                  mlstechwriter71 Level 1

                  Thank you again - it looks like this is going to work!

                  Very much appreciated!