views:

49

answers:

3

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 current time?

A: 

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

Boris
just hours and minutes. Sorry, it was 4am and I was on my way out :P
adamzwakk
@adamzwakk That's alright. If you still need help, then I can update my answer if you can be a bit more specific about how far you have got the times - i.e. if you're still just on having an XML file, or if you have them extracted as Strings, etc. Alternatively, if you found either of our answers satisfactory and have solved your problem, please mark one of them as correct by clicking on the 'tick' outline below the up/down voting icons. It helps the SO community know you've got your answer. Thanks!
Stephen
+2  A: 

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.

Stephen
A: 

Hey

I think you can get most of questions answered here: http://stackoverflow.com/questions/744057/how-can-you-save-time-by-using-the-built-in-date-class

Lillemanden