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