开发者

Comparing hours and minutes in AS3

开发者 https://www.devze.com 2023-01-08 13:57 出处:网络
Is there a way to compare the current time to a bunch of times (loaded from XML) and have it figure out which is the closest to the cu开发者_运维技巧rrent time?To expand on Boris\'s answer, you will i

Is there a way to compare the current time to a bunch of times (loaded from XML) and have it figure out which is the closest to the cu开发者_运维技巧rrent time?


To expand on Boris's answer, you will indeed do this via the Date class.

You will want to convert each of your XML-read dates into a Date object (aka, a representation based on the number of milliseconds since Jan 1, 1970), probably via the parse() static method of the Date class:

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

Once you have these date objects, you should create one more object for the current date/time, by calling the empty constructor Date(). Calling the valueOf() method on this new Date object will get you the number of milliseconds as above. Now you just have to loop through all of your XML dates and compare their value with the current date/time. The smallest difference is obviously the closest date/time.


you should be able to do this via the Date class in AS3. ( http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/index.html?Date.html& )

But I second Stephen's question : we can't help you any more without knowing your times format : are you comparing full text dates, timestamps, ...?


I think you can get most of questions answered here: How can you save time by using the built in Date class?

0

精彩评论

暂无评论...
验证码 换一张
取 消