动作日期比较 [英] Actionscript Date Comparison

查看:213
本文介绍了动作日期比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Actionscript code我有两个日期:

  VAR日期1:时间=新的日期(2011,1,1);
VAR DATE2:日期=新的日期(2011,1,1);
 

这不工作:

  VAR等于:布尔=日期1 ==日期2;
 

从阅读我发现,这是一个工作的替代,因为它只是变得毫秒数从时间基准点。

  VAR等于:布尔= date1.getTime()== date2.getTime();
 

所以,我的问题是:

  1. 为什么在动作上的日期不正常的相等运算符的工作?
  2. >和<运营商似乎做工精细,但它们可以被信任?
  3. 为什么他们的工作,但不是平等的经营者?
  4. 有没有一个标准的方法,我可以用比较日期,只是当返回-1,0,或1(我知道我可以轻松地创建自己的,但我宁愿使用现有的工具类)?

在此先感谢。

解决方案
  1. 因为日期是对象而不是本机的数据类型(类的实例),所以他们永远是不同的,除非它是不同的引用同一个实例。
  2. 也许,因为他们可以转化为原始格式(数字)相比(见<一href="http://help.adobe.com/en_US/as3/learn/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9c.html#WS5b3ccc516d4fbf351e63e3d118a9b90204-7f87">type转换的)。不知道这是否是被转换为数字或字符串,但?一定要测试
  3. 因为他们是不相等的;他们不是同一个对象。
  4. 您发(使用的getTime())是你可以用我想最好的办法比较。

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. 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.

这篇关于动作日期比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆