12 Replies Latest reply on Oct 5, 2016 4:34 AM by Pricoky

    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

                  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

                      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 = "";

                                 

                                    }

                                 

                                 

                                 

                                })();