【发布时间】:2010-07-25 08:54:22
【问题描述】:
有没有办法将当前时间与一堆时间(从 XML 加载)进行比较,并找出最接近当前时间的时间?
【问题讨论】:
-
您从 XML 中读取的这些时间/日期的格式是什么?
标签: apache-flex actionscript-3 date time flash-builder
有没有办法将当前时间与一堆时间(从 XML 加载)进行比较,并找出最接近当前时间的时间?
【问题讨论】:
标签: apache-flex actionscript-3 date time flash-builder
要扩展鲍里斯的答案,您确实可以通过 Date 类来做到这一点。
您可能希望通过 Date 类的 parse() 静态方法将每个 XML 读取日期转换为 Date 对象(也就是基于自 1970 年 1 月 1 日以来的毫秒数的表示):
// Taken from the linked webpage.
// Note there are many other formats that Date.parse supports, see the linked
// page for a list.
var dateParsed:String = "Sat Nov 30 1974";
var milliseconds:Number = Date.parse(dateParsed);
trace(milliseconds); // 155030400000
一旦你有了这些日期对象,你应该通过调用空构造函数Date()为当前日期/时间再创建一个对象。在这个新的 Date 对象上调用 valueOf() 方法将获得如上所示的毫秒数。现在您只需遍历所有 XML 日期并将它们的值与当前日期/时间进行比较。最小的差异显然是最接近的日期/时间。
【讨论】:
您应该能够通过 AS3 中的 Date 类来做到这一点。 (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/index.html?Date.html&)
但我同意斯蒂芬的问题:如果不知道您的时间格式,我们将无法再为您提供帮助:您是否在比较全文日期、时间戳......?
【讨论】:
我认为您可以在这里得到大部分问题的答案: How can you save time by using the built in Date class?
【讨论】: