7 Replies Latest reply on Aug 17, 2011 12:24 PM by Code Girl

    Comparing Dates

    Code Girl Level 1

      I have two datagrid columns which are a from date and to date.  I wanted to change the todate to the from date if the user changes the from date to a later date.

       

      Each record of the datagrid is an instance of a class and I have declared the valueObject class to an id.

       

      I did the comparason on a change event for the fromdate dateField.

       

      So, at first, I compared the class.todate < class.fromdate   But Flex didnt recognise this.

       

      So then I compared the outerDocument.datagridid.selectedItem.todate < outerDocument.datagrid.selecteditem.fromdate

       

      This sorta worked but I had to add getTime() to get it to work.  Well, sorta.

       

      Evidently the fromdate is not automatically updated when the dateField is selected and so here is what I ended up with.  Is this how it needs to be done or is there a better way?

       

       

       

      protected function dateField_changeHandler(event:CalendarLayoutChangeEvent):void

      {

       

       

      if (outerDocument.nonAvailDg.selectedItem.endDate.getTime() < startDateField.selectedDate.getTime())

      outerDocument.nonAvailDg.selectedItem.endDate =

      new Date(startDateField.selectedDate.toDateString() + " " + "5:00 PM");

      }

       

       

        • 1. Re: Comparing Dates
          kokorito Level 4

          use the time property of the date...

           

           

          Date.time():Number
          The number of milliseconds since midnight January 1, 1970, universal time,  for a Date object. Use this method to represent a specific instant  in time when comparing two or more Date objects.
          1 person found this helpful
          • 2. Re: Comparing Dates
            Code Girl Level 1

            I have been reading about Bubbling events.  Would it be better to bubble this event til it gets to the datagrid itself and then address the variables directly, or is there no real hit to calling outerdocument.datagrid......etc.

            • 3. Re: Comparing Dates
              Code Girl Level 1

              I just found a problem with the original solution.  Lets say someone selects a date but decides to change the date back one day in the field itself.  When the above event runs, it puts the selected date in the todate field or as in my code, the endDate field, instead of the hand entered field.  So I am back to figuring out a way to comparing the date in the field since apparently the event is triggered before the data is updated to the column field itself.  Which now makes me wonder if bubbling just might be my answer.  Maybe by bubbling the DateField will update the column data and then I can just compare the two columns directly rather than having to create a new Date object so I can compare.  Any suggestions?

              • 4. Re: Comparing Dates
                kokorito Level 4

                err, didnt understand that... but yes its better to pass data out of a component or class using an event (loose coupling) rather than using outerdocument.datagrid

                • 5. Re: Comparing Dates
                  Code Girl Level 1

                  So how do you do that?

                   

                  It turned out I really should have used the focus out event and I discovered I could access value directly within the initial event triggering but I am still having to use outerdocument.  So how do I accomplish this without using outerdocument?  Also, why am I not able to use the declared variable nonAvail directly?

                   

                   

                  <fx:Declarations>

                   

                    <valueObjects:NonAvailDto id="nonAvailDto">

                  .... more code

                   

                  </fx:Declarations>

                   

                   

                  Datagrid is instantiated

                   

                  protected function startDateField_focusOutHandler(event:flash.events.FocusEvent):void

                  {

                   

                   

                  if (outerDocument.nonAvailDg.selectedItem.endDate.time < value.time)

                  {

                  outerDocument.nonAvailDg.selectedItem.endDate = value;

                  outerDocument.nonAvailDg.selectedItem.endDate.setHours(17,00,00,00);

                  }

                  }

                  • 6. Re: Comparing Dates
                    Code Girl Level 1

                    I still want to know how you would do what you suggested

                     

                    However, I did some more code changing and this is what I now have.  Which one of the two is better and is there still a better way?

                     

                     

                     

                    protected function startDateField_focusOutHandler(event:flash.events.FocusEvent):void

                    {

                     

                     

                    if (outerDocument.nonAvailDto.endDate.time < value.time)

                    {

                    outerDocument.nonAvailDto.endDate = value;

                    outerDocument.nonAvailDto.endDate.setHours(17,00,00,00);

                    }

                    /* if (outerDocument.nonAvailDg.selectedItem.endDate.time < value.time)

                    {

                    outerDocument.nonAvailDg.selectedItem.endDate = value;

                    outerDocument.nonAvailDg.selectedItem.endDate.setHours(17,00,00,00);

                    }

                    */

                     

                     

                     

                     

                     

                    }

                    • 7. Re: Comparing Dates
                      Code Girl Level 1

                      For those who have been following this thread, I had tried to implement the bubbling event but it was giving me an error.  And now I know why and he it is

                       

                      Tip

                      Add a note hereEvent bubbling works only if the parent container declares the event you want to handle. For example, if you try to handle a change event from a ComboBox in a parent VBox in MXML, an error occurs because the compiler says there is no change event to listen for. To overcome this limitation, create your own custom component based on the container you want to use, and explicitly declare the selected event as a member of the new version of the container.