8 Replies Latest reply on Jun 18, 2016 6:30 PM by pdfUser1

    Count days backwards depending on year and month??

    pdfUser1 Level 1

      In my doc I have 7 text fields (Sun, Mon, Tues, Wed, Thurs, Fri, Sat). I use a different field to enter a date. When I enter a end date, I would like to have the 7 text fields to populate with the dates. For example, if I enter 6/18/2016 in the date field, I'd like to have the 7 text fields to populate like this, Sun would show 12, Mon would show 13, Tues would show 14, Wed would show 15, Thurs would show 16, Fri would show 17 and Sat would show 18. I say to count the days backwards is because I enter the week date end (always a Sat).

       

      What I'm running into is the month prior at times will have 30 days, some months have 31 (and of course leap year). So, for example, if I choose July 2, 2016 the days should come back like this, Sun should show 26, Mon should show 27, Tues should show 28, Wed should show 29, Thurs should show 30, Fri should show 01, Sat should show July 02.

       

      How can I do this correctly? I need a smart javascript to do it. Can someone please help me out with an example? Thanks.

        • 1. Re: Count days backwards depending on year and month??
          gkaiseril MVP & Adobe Community Professional

          JavaScript uses a Date object which is based on assigning a unique numeric value to each millisecond since midnight January 1, 1970 at the UTC (GMT) meridian. This results in a one to one mapping of each millisecond. That means one can easily count forward or backward using the JavaScript Date object and the various properties and methods of that object. Acrobat also adds some additional tools for processing the Date object. Have you looked at MDN JavaScript Reference and the Acrobat JavaScript API Reference?

           

          Adobe has also posted a number of tutorials about time calculations.

           

          In general, one converts the date string, yes, the visual representation of a date is a string and not a number, into a Date object. Then one can either add or subtract one day, either as a day unit or the number of milliseconds in a day, to get the next day or the previous day.

           

          A sample script for display the 7 days before a given date:

           

          console.show(); // open the JavaScript console;

          var cStart = "07/02/2016"; // the starting date string;

          var oStart = util.scand("mm/dd/yyyy", cStart); // convert date string to date object;

          var oAdjsted; // date object for adjustment;

          // count back 7 days and display each date's month and day;

          for(var i = 6; i > -1; i--)

          {

          oAdjusted = new Date(oStart.getTime()); // reset adjusted date to start date;

          oAdjusted.setDate(oAdjusted.getDate() - i); // adjust by -i days;

          console.println((i + 1) + ": " + util.printd("dddd mmm dd", oAdjusted)); // display short month and 2 digit date;

          } // loop back for next previous date;

           

          To be able to fill in field's we would need the field names and what value should appear in the field name. Careful planning of the field names could make the script simpler since it is possible to cmpute field names or make the field name a computible variable.

          • 2. Re: Count days backwards depending on year and month??
            pdfUser1 Level 1

            the field names are simply Sun, Mon, Tues, etc. The field where I would enter the date is named "endDate". I tried the script you provided but it keeps opening the debugger console. I don't understand what it's doing and why. I really don't know javascript all that much, but I can follow by example.

             

            How do I use the script? So the dates will calculate correctly and eliminate the 31st day where the month doesn't have 31 days? And where would I place the script? Is it a Custom Calculation? Or doc level, validate, etc?

            • 3. Re: Count days backwards depending on year and month??
              gkaiseril MVP & Adobe Community Professional

              It is an example of how to count down the dates and display the result for each incremental date. I would place this script in the "On Blur" action for the end date field. You can modify it so instead of printing to the console, you populate the field for each date. If your weekly interval includes dates in more than one year, you will also need to deal with the change in years.

               

              The code has comments explaining what each line does.

               

              Can you program n any language like Basic, Pascal, C?

               

              What do you know about creating forms and writing custom JavaScript calculations?

               

              Have you looked at any the Adobe provided tutorials?

               

              If this the start of a time sheet you will be dealing with the Date object for at least 14 more fields. I would also create a number of document level scripts to perform the repeated blocks of code like converting a date string or date and time string into the Date object, and formatting scripts to convert minutes to display hours and minutes.

              • 4. Re: Count days backwards depending on year and month??
                pdfUser1 Level 1

                I tried and tried but I'm totally lost. All the explaining in the world doesn't help. I'm not at that level of understanding to apply what you're saying.

                • 5. Re: Count days backwards depending on year and month??
                  Karl Heinz Kremer Adobe Community Professional

                  Can you please post your code and the error message you are getting on the JavaScript console.

                  • 6. Re: Count days backwards depending on year and month??
                    pdfUser1 Level 1

                    Hello Mr Kremer,

                    Thanks for responding. What I was getting at, is I just don't understand the script in the other post. It opens the console. I don't know what to do with the information there. I tried the script and cannot get it to work the way I want it to. So, after searching the Acrobat User forum I found this script which does work (to an extent).

                     

                    It calculates the days as I would like, but it also calculates the month and year. That part I don't want or need. I just want the Day. Take a look, can this script be modified to return just the day in the text field? I placed this script in each Day field (Sun thru Sat) as a Custom Calculation. I just know I'm getting closer.

                     

                    if (this.getField("Sat").value!="")

                    {

                    event.value=util.printd("mm/dd/yyyy",new Date(new Date(this.getField("Sat").value).getTime()-86400000))

                    }

                    else

                    {

                    event.value="";

                    }

                     

                    I don't know what the number (86400000) means or what it is referring to. It originally had a "+" in front of it. I changed it to a "-" to suite my needs. (Good guess, huh). Anyway, what do you think?

                    • 7. Re: Count days backwards depending on year and month??
                      George_Johnson MVP & Adobe Community Professional

                      Here's a link to a sample form that demonstrates how you can do this: http://acroscript.net/pdf/demos/populateWeek.pdf

                       

                      The script is in the Validate event of the date input field. Note that the day fields use a particular naming convention which allows some code simplification, so you'd have to use a similar pattern.

                      • 8. Re: Count days backwards depending on year and month??
                        pdfUser1 Level 1

                        Thank you Mr Johnson. That's perfect. More perfect than my ex-wife's hairdo.