9 Replies Latest reply on Dec 1, 2009 1:30 PM by djkhalif

    Advance date 90 days when date change in Form

    djkhalif Level 1

      Good evening all,

       

      I have this:

      <cfinput type="datefield" name="startdate" value="#dateformat(Now(), 'mm/dd/yyyy')#">

      <cfinput type="datefield" name="nextdate" value="#dateformat(Now() +90, 'mm/dd/yyyy')#" readonly="yes">

       

      I want to advance "nextdate" by 90 days  when "startdate" is changed.

        • 1. Re: Advance date 90 days when date change in Form
          BKBK Adobe Community Professional & MVP

          Use cfinput's bind attribute, for example


          <script type="text/javascript">

          /* add 90 days to a given date */
          addDays = function(dateStr) {
          var newdate = (new Date(dateStr)).add('d',90);
          var days = newdate.getDate();
          var months = newdate.getMonth()+1;
          var year = newdate.getFullYear();

          /* the datefield widget requires 2 digits to display*/
          if(days < 10) days = '0'+days;
          if(months < 10) months = '0'+months;
          return months + "/" + days + "/" + year;
          }
          </script>

           

          <cfform name="dateForm" id="dateForm">
          <cfinput type="datefield" name="startdate" value="#dateformat(Now(), 'mm/dd/yyyy')#">
          <cfinput type="datefield" name="nextdate" bind="javascript:addDays({dateForm:startdate})" readonly="yes"> 
          </cfform>

          • 2. Re: Advance date 90 days when date change in Form
            djkhalif Level 1

            BKBK,

             

             

            Thanks. This is what I was looking for. I made a slight change. I changed the second <cfinput type="text">, I left it readonly so that users cannot change the 90 day date.

             

             

             

            <cfform name="dateForm" id="dateForm">
            <cfinput type="datefield" name="startdate" value="#dateformat(Now(), 'mm/dd/yyyy')#">
            <cfinput type="text" name="nextdate" bind="javascript:addDays({dateForm:startdate})" readonly="yes"> 
            </cfform>

             

            Thanks again,

             

             

            djkhalif

            • 3. Re: Advance date 90 days when date change in Form
              djkhalif Level 1

              BKBK,

               

              I couldn't get it to work with flash format.

               

               

              THanks,

               

              djkhalif

              • 4. Re: Advance date 90 days when date change in Form
                BKBK Adobe Community Professional & MVP

                You're right. Flash doesn't wanna play. I'll have a look.

                • 5. Re: Advance date 90 days when date change in Form
                  BKBK Adobe Community Professional & MVP

                  I have been returning to the subject. I am still looking for a neat way to do dateAdd in Actionscript, as Coldfusion doesn't allow new Date(). (Coldfusion's flash forms don't allow you to create objects using the new keyword).

                   

                  In the meantime, why don't you post the "flash" version of your original question in the Rich Forms forum ? It's sure to attract new suggestions.

                  • 6. Re: Advance date 90 days when date change in Form
                    -==cfSearching==- Level 4

                    Yes, from what I recall there is not much in the way of date functions in Flex 1.5. But one nice thing is you can get away with some of the keywords like "new" using functions.  So you could try and roll your own.  I do not know if the date math works in all scenarios, but here is a quick example.  Note, I cheated and used the internal mx.formatters.DateFormatter() class.

                     

                    <cfform name="dateForm" id="dateForm" format="flash">
                        <cfformitem type="script">
                            function addDays(baseDate:Date, numOfDays:Number)
                            {
                                var newDate = new Date();
                                // calculate the days to add in milliseconds 
                                var daysToAdd = numOfDays * (24*60*60*1000);
                                // add the days to the starting date
                                newDate.setTime(baseDate.getTime() +  daysToAdd);
                                // format the new date as a string
                                var formatter = new mx.formatters.DateFormatter();
                                formatter.formatString = "MM/DD/YYYY";
                                return formatter.format(newDate);
                            }
                        </cfformitem>
                        <cfinput type="datefield" name="startdate" value="#dateformat(Now(), 'mm/dd/yyyy')#" />
                        <cfinput type="text" name="nextdate" bind="{addDays(startdate.selectedDate, 90)}" readonly="yes" />
                    </cfform>

                    • 7. Re: Advance date 90 days when date change in Form
                      BKBK Adobe Community Professional & MVP

                      @ -==cfSearching==-

                      Thanks for the new breakthough.

                       

                      @Djkhalif

                      Now you're spoilt for choice.

                       

                      <cfform name="dateForm" id="dateForm" format="flash">
                      <cfformitem type="script">
                      function addDays2(numberOfDays,dateObject) {

                          // convert date to milliseconds
                          var dateValue = dateObject.valueOf();   
                          var newDate = new Date(dateValue+numberOfDays*24*60*60*1000);

                          // remember getMonth returns 0 to 11 in Actionscript
                          return (newDate.getMonth()+1) + "/" + newDate.getDate() + "/" + newDate.getFullYear();
                      }
                      </cfformitem>
                      <cfinput type="datefield" name="startdate" value="#dateformat(Now(), 'mm/dd/yyyy')#" />
                      <cfinput type="text" name="nextdate" bind="{addDays2(90,startdate.selectedDate)}" readonly="yes" />
                      </cfform>

                      • 8. Re: Advance date 90 days when date change in Form
                        -==cfSearching==- Level 4

                        Thanks for the new breakthough.

                         

                        Haha

                         

                            var dateValue = dateObject.valueOf();   

                            var newDate = new

                        Date(dateValue+numberOfDays2460601000);

                         

                        Nice one. I suspected the constructor might accept milliseconds. But did not see it the documentation I found (granted it was old).

                         

                        -Leigh

                        • 9. Re: Advance date 90 days when date change in Form
                          djkhalif Level 1

                          BKBK and cfSearching,

                           

                          I apologize for getting back to so late. cfSearching example works.

                           

                           

                           

                          Thanks,

                           

                          djkhalif