2 Replies Latest reply on Nov 25, 2009 3:57 PM by Nyein Aung

    Script for Auto Date + 30 days

    Nyein Aung

      My company sends out date sensitive sales letters to our club members.
      Example: Today is dd/mm/yy, buy this product before dd/mm/yy and get $100 off.

       

      All these letters were designed in indesign CS4 and exported as PDFs (and than modified with form fields) so the company staff can edit the dates and print them as they wish.

       

      Is there a way to make it so when they load the document, one date is automatically changed to "today's" date and the rest of the date fields are automatically changed to the date of 30 days later?

        • 1. Re: Script for Auto Date + 30 days
          gkaiseril MVP & Adobe Community Professional

          You will need to use JavaScript programing to create a document level script and custom calculation.

           

          You can set the date of a field upon opening a PDF by using a document level JavaScript:

           

          // define the value for the date field

          var sField = 'myDateFieldName';

          // define the format of the date string

          var cFormatDate = 'mm/dd/yyyy';

          // get the field object to update

          var TodayDate = this.getField(sField);

          // get the date object

          var oDate = new Date();

          // set the field's value to the formatted date

          TodayDate.value = util.printd(cFormatDate,  oDate);

           

          To add 30 days to a date you need to do all the math in milliseconds and use a custom calculation field

           

          // define the value for the date field

          var sField = 'myDateFieldName'

          // define the format of the date string

          var cFormatDate = 'mm/dd/yyyy';

          // define some time constants

          var fSecond = 1000; // number of milliseconds in one second

          var fMinute = 60 * fSecond; // number of milliseconds in a minute

          var fHour = 60 * fMinute; // number of milliseconds in an hour

          var fDay = 24 * fHour; //number of milliseconds in a day

          // get the field object's string value

          var fTodayDate = this.getField(sField).value;

          // convert the string value into a date object

          var oDate = util.scand(cFormatDate, fTodayDate);

          // convert the date object to a value in milliseconds

          var fDate = oDate.getTime();

          // add 30 days to value using the number of milliseconds in a day

          var fNewDate = fDate + (30 * fDay);

          // convert computed date value to date object

          var oNewDate = new Date(fNewDate);

          // set the field's value to the date string for the date object

          event.value = util.printd(cFormatDate, oNewDate);

          1 person found this helpful
          • 2. Re: Script for Auto Date + 30 days
            Nyein Aung Level 1

            Hey,Thank you so much for that.unfortunately I can only get the today date to work.

            I went into page properties, selected run Java Script,copied and pasted the code you sent and changed the field names.

            I'm using a mac. Is there something I could be doing wrong?If you don't mind, could you walk me through how to set up the second date?I'm an absolute stranger to Java and programming.

            Thank you so much again.

             

             

             

            Date: Wed, 25 Nov 2009 07:57:23 -0700

            From: forums@adobe.com

            To: nyein.aung@hotmail.com

            Subject: Acrobat Scripting Script for Auto Date + 30 days

             

            You will need to use JavaScript programing to create a document level script and custom calculation.

             

            You can set the date of a field upon opening a PDF by using a document level JavaScript:

             

            // define the value for the date field

            var sField = 'myDateFieldName';

            // define the format of the date string

            var cFormatDate = 'mm/dd/yyyy';

            // get the field object to update

            var TodayDate = this.getField(sField);

            // get the date object

            var oDate = new Date();

            // set the field's value to the formatted date

            TodayDate.value = util.printd(cFormatDate,  oDate);

             

            To add 30 days to a date you need to do all the math in milliseconds and use a custom calculation field

             

            // define the value for the date field

            var sField = 'myDateFieldName'

            // define the format of the date string

            var cFormatDate = 'mm/dd/yyyy';

            // get the field object's string value

            var fTodayDate = this.getField(sField).value;

            // convert the string value into a date object

            var oDate = util.scand(cFormatDate, fTodayDate);

            // convert the date object to a value in milliseconds

            var fDate = oDate.getTime();

            // add 30 days to value

            var fNewDate = fDate + (30 * 24 * 60 * 60 * 60 * 1000);

            // convert computed date value to date object

            var oNewDate = new Date(fNewDate);

            // set the field's value to the date string for the date object

            event.value = util.printd(cFormatDate, oNewDate);

            >