Date Comparison in ActionScript

In my ActionScript code, I have two dates:

var date1:Date = new Date(2011,1,1); var date2:Date = new Date(2011,1,1); 

This does not work:

 var equal:Boolean = date1 == date2; 

From reading, I found this to be a working alternative, as it just gets milliseconds from a standard point in time.

 var equal:Boolean = date1.getTime() == date2.getTime(); 

So my questions are:

  • Why doesn't the normal equality operator work with dates in actionscript?
  • The ">" as well as the "<" operators seem to work fine, but can they be trusted?
  • Why do they work, but not the equality operator?
  • Is there a standard method that I can use when comparing dates that just return -1, 0, or 1 (I understand that I can easily create my own, but I would prefer to use an existing utility class)?

Thanks in advance.

+8
operators equality date actionscript
source share
2 answers
  • Since Dates are objects (instances of the class), instead of their own data type, therefore they will always be different, if only different references to the same instance.
  • Perhaps because they are converted to their own format (number) when comparing (see type of conversions ). Not sure if it has been added to a number or string? Be sure to check
  • Because they are not equal; they are not the same object.
  • The comparison you made (using getTime ()) is the best you can use, I think.
+6
source share

Re: # 4

You can use the ObjectUtil.dateCompare () function to get the results you are looking for.

mx.utils.ObjectUtil.dateCompare ()

+15
source share

Source: https://habr.com/ru/post/651321/


All Articles