2 Replies Latest reply on Aug 20, 2013 1:47 PM by gkaiseril

    Need help calculating and printing six sequential dates mm/dd/yy

    shisnet Level 1

      What is the proper JavaScript to take the date from a form field then print the next six dates (day+1) in corresponding day fields?

        • 2. Re: Need help calculating and printing six sequential dates mm/dd/yy
          gkaiseril MVP & Adobe Community Professional

          There can be lots of them depending upon the source of the starting date and how one wants to code the solution. It can be done using the number of milliseconds in a day or using one the set[DateUnit] methods.

           

          // get the string value of the starting date;
          var sDate = "20-Aug-2013";
          // convert to the date object
          var oDate = util.scand("d-mmm-yyyy", sDate);
          // convert date object to milliseconds from the Epoch date;
          var nDate = oDate.getTime();
          // milliseconds in one day;
          var nDay = 1000 * 60 * 60 * 24;
          // create date strings for today and the followng six days;
          var nNewDay = nDate;
          for(i = 0; i < 7; i++) {
          // add i days to starting date;
          nNewDay = nDate + (nDay * i);
          // convert new date value to date object
          oDate = new Date(nNewDay);
          // display result;
          console.println(i + ": " + util.printd("d-mmm-yyyy", oDate));
          } // end loop;

           

           

          Using the setDate method:

           

          // get the string value of the starting date;

          var sDate = "20-Aug-2013";

          // convert to the date object

          var oDate = util.scand("d-mmm-yyyy", sDate);

          // create date strings for today and the followng six days;

          for(i = 0; i < 7; i++) {

          // display result;

          console.println(i + ": " + util.printd("d-mmm-yyyy", oDate));

          // adjust date object by 1 day i times;

          oDate.setDate(oDate.getDate() + 1);

          } // end loop;