Actionscript Date Comparison

泄露秘密 提交于 2019-12-06 23:07:06

问题


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 doesn't work:

var equal:Boolean = date1 == date2;

From reading I've found that this is a working alternative since it just gets the number of milliseconds from a standard point in time.

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

So my questions are:

  1. Why doesn't the normal equality operator work on Dates in actionscript?
  2. ">" as well as "<" operators seem to work fine, but can they be trusted?
  3. Why would they work but not the equality operator?
  4. Is there a standard method I can use when comparing dates that just returns a -1, 0, or 1 (I realize I can easily create my own, but I would rather use an existing utility class)?

Thanks in advance.


回答1:


  1. Because Dates are objects (instances of a class), instead of a native data type, so they'll always be different unless it's different references to the same instance.
  2. Probably, because they get transformed to a native format (number) when compared (see type conversions). Not sure if it's being cast to a number or a string though? Be sure to test
  3. Because they're not equal; they're not the same object.
  4. The comparison you made (using getTime()) is the best thing you can use I guess.



回答2:


Re: #4

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

mx.utils.ObjectUtil.dateCompare()



来源:https://stackoverflow.com/questions/4753046/actionscript-date-comparison

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!