34 Replies Latest reply on Aug 16, 2018 1:37 PM by oli549

    Add x days to x date

    freelance_java_n00b Level 1

      Hi!

       

      I am quite new at Acrobat and Javascript, which is a common lament on this discussion board from what I have read. But I need a little help!

       

      I am creating a form in which the user needs to be able to input the estimated end date in the first field, and then input the number of days added in the next field, followed by an automatically calculated adjusted end date in the third field.

       

      "OriginalDate" + "DaysAdded" = "NewDate"

       

      Any help or links to help would be fantastic!!

       

      Thank you!

        • 1. Re: Add x days to x date
          George_Johnson MVP & Adobe Community Professional

          The custom Calculate script for the NewDate field could look something like the following:

           

          // Custom Calculate script for NewDate field

          (function () {

           

              // Get date entered into the OrigianlDate field

              var sDate = getField("OriginalDate").valueAsString;

           

              // Convert string to date

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

           

              // Get the number of days added, as a number

              var days = getField("DaysAdded").value;

           

              // Add days to date

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

           

              // Populate this field with the result

              if (sDate) {

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

              } else {

                  event.value = "";

              }

           

          })();

           

          This will set the end date to the same as the original date if the days added field is blank (or 0). If you want it to behave differently, post again.

          • 2. Re: Add x days to x date
            George_Johnson MVP & Adobe Community Professional

            Also, change the date format strings in the code above to match what you're using for the date fields.

            1 person found this helpful
            • 3. Re: Add x days to x date
              freelance_java_n00b Level 1

              George,

               

              Thank you so so very much for your assistance. This is probably a walk in the park for you, but I was really struggling with this one. In my hunt for an answer, I ended up going down a lot of rabbit trails.

               

              Thank you!!

               

              -Dan

              • 4. Re: Add x days to x date
                freelance_java_n00b Level 1

                Hi George,

                 

                So I have a new dilemma related to the previous question.

                 

                Is it possible to create a script based on a 5-day work week instead of a 7-day week? I don't think this is possible, but we were trying to find a way to input a calculation that would account for weekends.

                 

                Thanks for your help!

                • 5. Re: Add x days to x date
                  ChristophLindemann Level 1

                  Thank you for the help!

                   

                  CEO [ Mod: Personal information removed ]

                  • 6. Re: Add x days to x date
                    gkaiseril MVP & Adobe Community Professional

                    Yes, but you would need to access the day of the week through the "getDaty()" method and add 2 for Satrudays and 1 for Sundays to bump the date to the next Monday.

                     

                    // Custom Calculate script for NewDate field

                    (function () {

                     

                        // Get date entered into the OrigianlDate field

                        var sDate = getField("OriginalDate").valueAsString;

                     

                        // Convert string to date

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

                     

                        // Get the number of days added, as a number

                        var days = getField("DaysAdded").value;

                     

                        // Add days to date

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

                       // possible adjustment for weekend

                       var nAdj = 0;

                        // get the zero based day of the week starting on Sunday

                        var nDay = d.getDay();

                        // test for Sunday or a value  day value of 0

                         if(nDay == 0) nAdj = 1; // bump to Monday

                        // test for Saturday or a day value of 6

                        if(nDay == 6) nAdj = 2; // bump to Monday

                       // adjust date

                       d.setDate(d.getDate() + nAdj);   // adjust computed date by needed bump

                        if (sDate) {

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

                        } else {

                            event.value = "";

                        }

                     

                    })();

                    1 person found this helpful
                    • 7. Re: Add x days to x date
                      GuyAus Level 1

                      I have a very similiar question. I have a field which has dates entered manually ("datefield"), a drop down box with 4 options ("cattype"), which depending on the option, (cat1, cat2, cat3,cat4) needs to add certain amount of days from "datefield" (cat1=1 day, cat2-2 days, cat3=2 days, cat4=15 days) to another field ("duedate"). Is this possible with a drop down box or should I use radio buttons? eg: "datefield"+"cat1"="duedate".

                       

                      Many thanks in advance.

                      • 8. Re: Add x days to x date
                        Pricoky Level 1

                         

                        • 9. Re: Add x days to x date
                          Karl Heinz Kremer Adobe Community Professional

                          On my calendar, when I add 30 days to 9/24, I end up on 10/24, so what you want is to add 29 days - just add the script so that it always adds 29 days - or, if you have a field that indicates how many days to add, subtract one from the value of that field. You can do that for example by changing this  line:

                           

                          // Get the number of days added, as a number
                          var days = Number(getField("DaysAdded").value) - 1;
                          
                          • 10. Re: Add x days to x date
                            Pricoky Level 1

                            Thank you Karl. The thing is that I work for a Medical Care Center and I have to create a file for each pacient we have. The "added days" value is the period of time/days each pacient is treated for so I'll have to see "30" in that field or sometimes "15" but the script should also calculate the day that the pacient beginns with along with the end date. I'll try to add the lines you've suggested and see what happens! Thank you!

                            • 11. Re: Add x days to x date
                              try67 MVP & Adobe Community Professional

                              In that case you need to add 1 to the number of days added...

                              • 12. Re: Add x days to x date
                                Pricoky Level 1

                                Thank you guys, I've modified the line and it's all good now. It is even working with the calendar gadget from www.formrouter.com as long as the date format looks the same as originally set in the gadget. That's how the working script looks like:

                                 

                                // Custom Calculate script for NewDate field

                                 

                                (function () {

                                 

                                 

                                 

                                    // Get date entered into the OrigianlDate field

                                 

                                    var sDate = getField("Data inceput.0").valueAsString;

                                 

                                 

                                 

                                    // Convert string to date

                                 

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

                                 

                                 

                                 

                                    // Get the number of days added, as a number

                                 

                                    var days = getField("Nr. zile.0").value - 1;

                                 

                                 

                                 

                                    // Add days to date

                                 

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

                                 

                                 

                                 

                                    // Populate this field with the result

                                 

                                    if (sDate) {

                                 

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

                                 

                                    } else {

                                 

                                        event.value = "";

                                 

                                    }

                                 

                                 

                                 

                                })();

                                • 13. Re: Add x days to x date
                                  davidg17220393 Level 1

                                  Hello,

                                   

                                  Not sure if this is the right thread, but I am trying to have a field add 7 days to the date entered on another field. Is this possible?

                                  • 14. Re: Add x days to x date
                                    try67 MVP & Adobe Community Professional

                                    Did you read the thread you're replying to? Your question is answered in it...

                                    • 15. Re: Add x days to x date
                                      davidg17220393 Level 1

                                      Yes, but still not sure which one, sorry for my ignorance.

                                      • 16. Re: Add x days to x date
                                        try67 MVP & Adobe Community Professional

                                        The one marked as correct is a good starting point...

                                        • 17. Re: Add x days to x date
                                          davidg17220393 Level 1

                                          I got it to work, BUT the field I want it to give me 7 days from now still displays the original date I entered.

                                          • 18. Re: Add x days to x date
                                            try67 MVP & Adobe Community Professional

                                            Check the JS Console for error messages. Also, you have to change the field's value for the script to "kick in".

                                            • 19. Re: Add x days to x date
                                              davidg17220393 Level 1

                                              This is what I have:

                                               

                                              // Custom Calculate script for NewDate field

                                              (function () {

                                               

                                                  // Get date entered into the OrigianlDate field

                                                  var sDate = getField("Date").valueAsString;

                                               

                                                  // Convert string to date

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

                                               

                                                  // Get the number of days added, as a number

                                                  var days = getField("Availability Date").value + 7;

                                               

                                                  // Add days to date

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

                                               

                                                  // Populate this field with the result

                                                  if (sDate) {

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

                                                  } else {

                                                      event.value = "";

                                                  }

                                               

                                              })();

                                               

                                              If I enter today's day I get 5/26/2018?

                                              • 20. Re: Add x days to x date
                                                try67 MVP & Adobe Community Professional

                                                Why all the extra fields' values? Just use this:

                                                 

                                                var d = new Date(); // This is the current date
                                                d.setDate(d.getDate() + 7); // Add 7 days to it
                                                event.value = util.printd("mm/dd/yyyy", d); // Apply it to the field's value
                                                
                                                • 21. Re: Add x days to x date
                                                  w0113 Level 1

                                                  Can you help me please.  I have a date - Date received and I am trying to get it to automatically calculate the deadline - deadline2 three days after the date received.

                                                   

                                                  This is what i entered but nothing happens - nothing populates when i put in the date received

                                                   

                                                  // Custom Calculate script for Deadline2 field

                                                  (function () {

                                                   

                                                      // Get date entered into the DateReceived field

                                                      var sDate = getField("DateReceived").valueAsString;

                                                   

                                                      // Convert string to date

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

                                                   

                                                      // Get the number of days added, as a number

                                                      var days = getField("DaysAdded").value;

                                                   

                                                      // Add days to date

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

                                                   

                                                      // Populate this field with the result

                                                      if (sDate) {

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

                                                      } else {

                                                          event.value = "";

                                                      }

                                                   

                                                  })();

                                                  SyntaxError: syntax error})();

                                                  • 22. Re: Add x days to x date
                                                    try67 MVP & Adobe Community Professional

                                                    I hope that last line is not a part of your code... Assuming it's not I would change this line:

                                                    var days = getField("DaysAdded").value;

                                                    To:

                                                    var days = Number(this.getField("DaysAdded").value);

                                                     

                                                    Then try again. If it still doesn't work, check the JS Console for errors.

                                                    • 23. Re: Add x days to x date
                                                      w0113 Level 1

                                                      the last line is the error I get when I check the JS console for errors

                                                       

                                                      This may be a dumb question but because I've not done this before I am going to ask... I am entering the above script on the field where i want the new date to populate  under Text field properties, calculate, using custom calculation script.  Is that the correct place. 

                                                       

                                                      Nothing is populating in the field when i enter the Date received?  Is there i script i need to add to the date received field?

                                                      • 24. Re: Add x days to x date
                                                        try67 MVP & Adobe Community Professional

                                                        Yes, that is correct, and that's all you need to do.
                                                        I'm not getting the same error message when I copy your code... Are you sure you're copying the whole thing?

                                                        • 25. Re: Add x days to x date
                                                          w0113 Level 1

                                                          I am copying it exactly to the end and I cannot get it to populate the new date i have no idea what is wrong.

                                                          • 26. Re: Add x days to x date
                                                            try67 MVP & Adobe Community Professional

                                                            Your image did not go through... You need to use the Insert Image icon to add it.

                                                            • 27. Re: Add x days to x date
                                                              w0113 Level 1

                                                              Untitled.png

                                                              • 28. Re: Add x days to x date
                                                                try67 MVP & Adobe Community Professional

                                                                I can't see the full code in that screenshot... Also, what version of Acrobat is that?

                                                                • 29. Re: Add x days to x date
                                                                  w0113 Level 1

                                                                  Nuance Power PDF Advanced

                                                                  Product Information

                                                                  Version: 2.1

                                                                  • 30. Re: Add x days to x date
                                                                    try67 MVP & Adobe Community Professional

                                                                    OK, that might explain it... You're not using Adobe software, so it's possible this software simply can't handle the script correctly, or has other problems with it (like not supporting some of the methods used in it). I suggest you try posting in a forum for Nuance.

                                                                    To increase your chances of it working I would remove the first and last lines of the code, ie the function definition. It's not really necessary and just complicates things.

                                                                    • 31. Re: Add x days to x date
                                                                      w0113 Level 1

                                                                      Thank you

                                                                      • 32. Re: Add x days to x date
                                                                        oli549 Level 1

                                                                        Hi,

                                                                         

                                                                        I'm trying to add a number of business days to a certain date field but the formula used to ''bump'' to the next Monday only works for the first week-end. In other words, if i have a certain date (let's say 08-16-2018) and want to add 10 business days, the calculation :

                                                                        // test for Sunday or a value  day value of 0

                                                                         

                                                                             if(nDay == 0) nAdj = 1; // bump to Monday

                                                                         

                                                                        will give me 08-27-2018 when in fact it should be the 08-30-2018. What can i add to the formula so it exclude every week-ends and not only the first one. Many thanks !

                                                                        • 33. Re: Add x days to x date
                                                                          gkaiseril MVP & Adobe Community Professional

                                                                          You need to add the number of days that are included within the 10 business day period. For your example there are 2 weekends or 4 days, Saturday and Sunday. But you will need to also add code for when the start day is on a Monday and there is only one weekend or 2 days within the 10 business day span. And the computation does not provide for any holidays.

                                                                          • 34. Re: Add x days to x date
                                                                            oli549 Level 1

                                                                            Thanks a lot for you help. Is there a specific coding line i could add to get this result ?