4 Replies Latest reply on Jun 22, 2006 9:24 AM by KellyIPZ

    Date Compare

      It appears that in AS3, Dates are compared by refrence instead of by value. I have a function i've written that compares 2 dates using the time property to give an accurate result, but this is makes for very messy code since i have to check if i'm dealing with a date and then put a case in to deal with this. This is also going to be VERY confiusing for new users of Flex.

      If anyone on the Flex team reads this post, it would be MUCH better if you could compare two dates using (d1 == d2) and get an accurate result based on the underlying (time) value.

      - Kelly Birr
      Agents Ally, Inc
        • 1. Re: Date Compare
          ntsiii Level 3
          Have you tried "==="?

          === strict equality Tests two expressions for equality; the strict equality (=== )operator performs in the same way as the equality (== ) operator, except that data types are not converted.

          • 2. Re: Date Compare
            KellyIPZ Level 1
            Yes, I've tried "===". It evaluates by reference also. Adobe has a DateUtil class in the AS3 'corelib' library that will let you perform a true evaluation on dates.

            The real reason for this topic was not so much a question as a request for enhancement in case any of the Flex development team monitors this forum.

            I do understand the technical reasons for this behavior. Still, having to write special cases in all my code to deal with diffrent evaluation rules of Dates vs. Numbers and Strings is unpleasant, counter-productive and will be confusing for new developers. I believe it would be of great benefit to the product if date classes would compare by their internal .time value by default.

            - Kelly

            • 3. Re: Date Compare
              Let me make sure that I understand this correctly. According to the documentation "In ActionScript 3.0, all arguments are passed by reference because all values are stored as objects. However, objects that belong to the primitive data types, which includes Boolean, Number, int, uint, and String, have special operators that make them behave as if they were passed by value."

              The fact that the == operator doesn't evaluate on an object makes sense to me. Are you asking then that the date value be treated as a primitive data type? Which aspect of the date object would be the default for evaluation by value? Exact match down to the millisecond? I think that this may be one reason that date was not implemented as a primitive because there isn't a clear default level of detail for comparison.

              As far as the code being messy: is this because you have to check for special instances of data types as you move through a mixed data set and are doing brute comparisons? I'm thinking that you could easily create a single function to wrap that logic and keep the higher level pass through the data cleaner from a code perspective. Not knowing the problem that you are trying to solve it is a bit more challenging to figure out how to best help out with this. If you can post something more detailed about where you are running into the problem perhaps someone can suggest a more elegant solution.

              Best regards,

              • 4. Re: Date Compare
                KellyIPZ Level 1

                Thanks for your reply. I do understand that the Date is a reference object and that is why it is compares the way it does. But the quick answer to your question is “Yes” I would think that AS3 would be a better language if Date objects were [at least] compared as primitives. I had not given much thought to assignment by value vs. reference but since you bring it up, I believe that would be better also. So again “Yes” I believe that if Date objects were treated as primitives, AS3 would be made simpler and more efficient.

                As far as what component should be used for comparison, I believe that since internally the Date seams to be wrapper around its numeric .time value then that value (in milliseconds) should be used for evaluation. It’s easy enough to write code manually to do this comparison but does require special cases, and checking for null.

                For Example; I have an ObjectMonitor class that watches ObjectsProxys and builds a change set of modified properties to post back to a web service. For primitive types this is simple enough I can compare :

                If (myObject[event.property].oldValue == event.newValue) {


                This works very well for strings, Booleans and numbers. But with Dates in the mix, the same line of code becomes.

                if (myObject[event.property] is Date) {
                if ( (myObject[event.property] != null) && (event.newValue != null) ) {
                if ( (myObject[event.property] as Date).time == (event.newValue as Date).time) {

                else {

                else {
                if (myObject[event.property].oldValue == event.newValue) {


                This is a mess, and yes my code is actually a bit cleaner than this, I have a dateCompare() function. Still having to put this special case in even this one class is “messy”.

                This will also be very confusing to new Flex developers moving from other languages, especially any .NET languages where DateTime is a value type.

                - Kelly