10 Replies Latest reply on Jan 24, 2011 1:50 AM by arulmurugan77

    How to loop or iterate through a Datechooser

    Devtron Level 3

      I am having some trouble figuring out how to loop through a DateChooser.


      Basically, I need to identify all matching "day" values.



      var _mymatching.day = 4;


      for each( date in calendar ) {


               if (date.day = _mymatching.day)




      My problem is I do not understand how to loop through the Calendar/DateChooser, for each day in the month.


      Any ideas?

        • 1. Re: How to loop or iterate through a Datechooser
          Devtron Level 3

          I suppose it isn't *necessary* to loop through a DateChooser's UI objects. I suppose it would be easier to create a loop based on date values or a date range.


          But I am curious how FLEXIBLE FLEX is with DateChooser....any takers?

          • 2. Re: How to loop or iterate through a Datechooser
            rtalton Level 4

            Depends on what you're trying to accomplish. You can, for example, enable/disable dates or ranges of dates, based on your matching date. It is very flexible.

            • 3. Re: How to loop or iterate through a Datechooser
              Devtron Level 3

              Thanks but my psuedocode explains what I want to do.


              I simply want to loop through the dates. I can handle the rest of the functionality.


              What I am really trying to do is color/paint each matching day. Say I pick Weds Oct 4th. I want all the Weds in October to get painted a certain color.


              I've got the painting working and I understand the dateCollection and ranges, but none of those help me loop through or iterate the days for the month.


              I was hoping to do a simple object for each loop. Instead, it looks like I should do a date-range-loop and declare way too many variables to make that happen. I was looking for a FLEXIBLE solution, not re-inventing the wheel here...


              I tried this but it didnt work:

              for each( var obj:Object in myDateChooser ) {




              I also looked at this but it no workey either:

              for(var i:uint = 0; i < myDateChooser.numChildren; i++) {




              One would think that DateChooser control would have a collection to work with. I guess not?

              • 4. Re: How to loop or iterate through a Datechooser
                rtalton Level 4

                Without sounding too insistent, please check out the disabledRanges and selectedRanges properties of DateChooser. This will enable you to disable ("gray-out") the days which you do not want to highlight. You can use selectedRanges to select all appropriate dates. The selectionColor style can be used to color the selected dates' text as you wish. The selectionIndicatorSkin will color the highlight area of all selected dates. The DateChooser is quite versatile.


                All this without reinventing anything.

                • 5. Re: How to loop or iterate through a Datechooser
                  Devtron Level 3

                  1. Disabled ranges do not apply. I do not use disabled dates. all dates MUST be enabled.

                  2. Enabled range might actually work. Oh wait a second, there is no such thing...

                  3. I do not need to disable or "grey out" any days. So that is irrelevant.

                  4. selectedRanges will not work because we will not be using SELECTION here. only mouse events/click events. SELECTION is pretty worthless and requires the user to use keyboard/mouse combinations that are not intuitive. My requirements are to never use that.

                  5. The selectionIndicatorSkin is irrelevant too. Like I already said, I have the coloring working. No skin needed.


                  AGAIN, what I am trying to accomplish is to SIMPLY loop through all 30 days of one month. Without having to use a Date Range function or date strings.


                  Here is what I have that is somewhat working and YES AS YOU CAN SEE, this is waaaaaaaaaay too much code to simply loop through one month's set of days to find matching days throughout the month:


                                  var _selectedDate:Date = calendar.selectedDate;
                                  var _dayPosition:Number = _selectedDate.day;
                                  var startMonth:Number = calendar.displayedMonth + 1;
                                  var startYear:Number = calendar.displayedYear;
                                  var endMonth:Number = calendar.displayedMonth + 2;
                                  var endYear:Number = calendar.displayedYear;   
                                  //reset month value for December, since Date objects are not 0 based, this is WACK!!
                                  if (endMonth>12) {
                                      endMonth -= 12;
                                  var startDate:Date = DateField.stringToDate( startMonth.toString() + "/01/" + startYear.toString(),"MM/DD/YYYY");
                                  var endDate:Date = DateField.stringToDate(endMonth + "/01/" + endYear ,"MM/DD/YYYY");
                                  var _lastMonthDate:Date = DateField.stringToDate(endMonth + "/01/" + endYear ,"MM/DD/YYYY");
                                  _lastMonthDate.setDate( endDate.getDate() - 1);


                                  var _dateDiffCount:Number = calculateDays(startDate, _lastMonthDate);
                                  for (var i:int=0; i < _dateDiffCount; i++) {


                                      //increment start date
                                      var _currentDate:Date = DateField.stringToDate(startMonth.toString() + "/01/" + startYear.toString(), "MM/DD/YYYY");
                                      _currentDate.setDate(startDate.getDate() + i);
                                      if (_currentDate.day == _dayPosition) {
                                          //write even here
                                          trace( '_currentDate-->' + _currentDate.toDateString() );
                                          trace( 'we have a match folks!' );




                  ^ That is approximately 40 lines of code, that could be easily contained in a for loop on the UI object, if one actually existed. Do you know of one maybe I dont? That is what I am hoping for but you keep repeating the basics.


                  Could you show me how to do this with selectedRanges, since this is apparently so easy?


                  Even though we technically do not require date selection (CTRL + CLICK) from the user?


                  By the way, this is an extended Calendar component. Each dateChooser is only limited to one month, via CSS hack I discovered by disabling the month arrows. (WHAT A P.O.S.!!)


                  Here is a screenshot to show you that all the functionality already exist:




                  Once again, I am not asking for functionality, I am asking for iteration help with datechooser control. Do you know how to iterate through a dateChooser control? Or do I really need to write all these date hacks to find the beginning and end dates of my calendar months?

                  • 6. Re: How to loop or iterate through a Datechooser
                    rtalton Level 4

                    I can tell you are a very frustrated person. Maybe someone else can help you get through your issues.

                    • 7. Re: How to loop or iterate through a Datechooser
                      Devtron Level 3

                      you're probably right. I doubt you will admit it, but that is too much code to do something so trivial and simple, such as loop through a calendar control.


                      and yes, FLEX gets me frustrated.


                      what is also frustrating is when people try to give you "its really quite easy" advice and dont even read the question.


                      So back to the question at hand...does ANYONE know how to iterate through the days of a Datechooser control?



                      The code should look more like this:
                      for each( var obj:Object in myDateChooser ) {


                      if (obj.day == calendar.selectedDay.day)
                            Alert.show('match found, lets paint!');




                      ^ it should only be a few lines of code. not my 40+ lines of date string hacks. I sincerely hope that makes sense.

                      • 8. Re: How to loop or iterate through a Datechooser

                        for(var i:int=0;i<dateGrid.numChildren;i++)






                                       if(obj.text == "14" || obj.text == "18")


                                            //do something to day 14 or day 18 object....






                        • 9. Re: How to loop or iterate through a Datechooser
                          Devtron Level 3

                          ^ wow, thank you...

                          • 10. Re: How to loop or iterate through a Datechooser

                            Hi Devtron,


                            Did it worked for you can you pls share the code.