10 Replies Latest reply on Feb 4, 2011 7:18 AM by Pablo Souza

    How do I compare two dates?

    Jerry62712 Level 1

      First, yes I have checked the forum for prior discussions.  There were four pages of them and I got through 2 before I gave up.  I also checked the API, but couldn't find an example of what I'm doing.

       

      A date object was created in Actionscript.  It looks like this:

          var holdDate:Date = new Date(2010,6,30);

       

      I have a date control on a form.  It looks like this:

         <DHSCntl:fiRightLabel id="dfFirstContactDate"
             label="First Contact Date:"
             labelWidth="130"
             width="30%">
             <DHSCntl:DateFieldExt id="DATEOFFIRSTCONTACT"
                 width="130"
                 yearNavigationEnabled="true"
                 formatString="MM/DD/YYYY"
                 restrict="0-9/"
                 editable="true"
                 tabIndex="15"
                 change="dfFormUpdated()"/>
         </DHSCntl:fiRightLabel>

       

      The extention allows the control to have an editable field as well as a year change like the builtin month one.

       

      This control is populated by the user.  If the control is before the hold date then one set of actions happen.  The problem is I can't compare the two.  I've tried casting the control as Date, using various properties and finally am going to extract the month, day and year to test.  I think there has to be a very simple way to compare two date objects and I just can't see it.  But I hope you can.

        • 1. Re: How do I compare two dates?
          Andrei Kouzmenkov

          It's not quite clear what are you trying to compare.

          If DHSCntl:DateFieldExt control is an extension of mx:DateField control, then use DateField.selectionDate which is of Date type to compare with holdDate.

           

          Then there are a number of ways to compare Dates. The simplest one is to Date.getTime() which gives you milliseconds.

          But if you are not interested in time part, which can bring complications due to time zones, it's better to have your own custom function, which compares dates by year, month and date.

           

          Also look for as3commons-lang library which has DateUtils with many useful functions.

           

          Hopefully that will help.

           

          Andrei.

          • 2. Re: How do I compare two dates?
            Jerry62712 Level 1

            I have this code:

                var holdDate:Date = new Date(2010,6,30);
                if (compDF.DATEOFFIRSTCONTACT.selectedDate as Date > holdDate)

            which doesn't work.  That is, I have 5/1/2010 in the control and it doesn't fall into the "if" correctly.  I have 7/1/2010 in the control and it does the same thing which is incorrect.

            • 3. Re: How do I compare two dates?
              Andrei Kouzmenkov Level 1

              For simple cases you can try to use getTime():

              if (compDF.DATEOFFIRSTCONTACT.selectedDate.getTime() > holdDate.getTime()){

              ...

              }

               

              Given you are sure selectedDate is never null and DATEOFFIRSTCONTACT is always a different date than holdDate.

               

              If year/month/date may be equal, time part can introduce some surprises. In such case it's better to write a custom function like:

               

               

              if (d1.fullYear != d2.fullYear)

                  return d1.fullYear > d2.fullYear ? 1 : -1;

              else if(d1.month != d2.month)

                  return d1.month > d2.month ? 1 : -1;

              else if(d1.date != d2.date)

                  return d1.date > d2.date ? 1 : -1;

              else

                  return 0;

               

              Andrei.

              • 4. Re: How do I compare two dates?
                Jerry62712 Level 1

                I think I have stumbled on the problem/solution.

                 

                The test of the date wasn't working right.  I decided to move the control to a new date object so both dates (the fixed one and the one from the form) would be in the same format.  Then when I stepped through it looking at the watched expressions, I noticed the date wasn't right.

                 

                It looks like the month part is zero based so when I put in "7/1/2010" it was not greater than "6/30/2010".  It showed up as "6/1/2010" when you look at Month, Date and FullYear.  So I added one to the Month part of the "formDate" object I created and it fell into the code as it should have.

                • 5. Re: How do I compare two dates?
                  Andrei Kouzmenkov Level 1

                  Yes, month is zero-based, which is annoying in most cases.

                  • 6. Re: How do I compare two dates?
                    Pablo Souza Level 3

                    You can also compare two dates like below.

                    <?xml version="1.0" encoding="utf-8"?>
                    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                          xmlns:s="library://ns.adobe.com/flex/spark"
                          xmlns:mx="library://ns.adobe.com/flex/mx">
                    <fx:Script>
                      <![CDATA[
                      
                       protected function button1_clickHandler(event:MouseEvent):void
                       {
                        var currentDate:Date = new Date();
                        currentDate.setHours(0,0,0,0);
                        var selectedDate:Number = Date.parse(date.text);
                       
                        if(currentDate.valueOf() > selectedDate.valueOf())
                        {
                         result.text = "the entered date is lower than the current date";
                        }
                        else
                        {
                         result.text = "the entered date is higher than the current date";
                        }
                       }

                      ]]>
                    </fx:Script>

                    <s:layout>
                      <s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/>
                    </s:layout>

                    <s:Label text="YYYY/MM/DD"/>
                    <s:TextInput id="date" text="2011/02/01"/>
                    <s:Button label="Compare with current date" click="button1_clickHandler(event)" />
                    <s:Label id="result" />

                    </s:Application>

                     

                    Hope it helps you!

                     

                     

                    Regards,

                    Pablo Souza

                    1 person found this helpful
                    • 7. Re: How do I compare two dates?
                      Jerry62712 Level 1

                      Belated (we were shut down yesterday) thanks!  I've never used the date.parse method.  It looks like it converts the date to a number thus getting around 12/1/2001 might be greater than 1/1/2011 on a string comparison.

                      • 8. Re: How do I compare two dates?
                        Pablo Souza Level 3

                        Nice! Have you solved the problem now?

                         

                         

                        Regards,

                        Pablo Souza

                        • 9. Re: How do I compare two dates?
                          Jerry62712 Level 1

                          Yep, and a big thanks to everyone that took the time to help me on this!  Just talking it out is all that is needed sometimes.  I don't mind displaying my ignorance as someone else may benefit - we can't all be the gurus.

                          • 10. Re: How do I compare two dates?
                            Pablo Souza Level 3

                            You are absolutely sure!

                             

                             

                            Regards,

                            Pablo Souza