views:

95

answers:

2

Hi,

I have a XML file with a few concert dates. In my flash/AS3 file, I would like to show only the up coming event, relative to current date.

Like this:

Event 01: 30-05-2010

Event 02: 02-06-2010

Event 03: 05-06-2010

Today is 28-05-2010, so I need to list Event 01. On 01-06-2010 I need to list Event 02.

I have the basic AS3 code for listing the XML working, but I'm having trouble filtering the result.

Any ideas? Thanks.

+1  A: 

Construct Date objects for each date and compare their getTime() values - it's the number of milliseconds passed since 1st Jan, 1970.

Amarghosh
thank you Amarghosh. In the meanwhile I came up with another way, so I didn't try yours... But thanks anyway!
fana
A: 

I guess this works:

var my_date:Date = new Date();
var dia:Date = new Date(my_date.fullYear,my_date.month,my_date.date);
//var dia:Date = new Date(2010,05,06);
var nowEvent1:Date = new Date(2010,04,30);
var nowEvent2:Date = new Date(2010,05,02);
var nowEvent3:Date = new Date(2010,05,05);
var nowEvent4:Date = new Date(2010,05,06);

var eventArray:Array = [nowEvent1,nowEvent2,nowEvent3,nowEvent4];

for (var i:int; i < eventArray.length; i++) {
    if (eventArray[i] >= dia) {
        trace(eventArray[i]);
        return;
    }
}

It's just a sample with hardcoded values (because the XML code is too big) but it seems to be working. Changing the "dia" variable returns the correct up coming event date.

In my code I get the current date from a flashvar setted by PHP and then convert it to a date object in flash.

I can post the whole code if someone's interested. And I'm oppened to a better way too!

fana
Are you sure that the dates in the array will be sorted? This won't work if the dates aren't sorted. Also, I am not sure how the comparison works - does it use `toString` (in which case you should not use this - it'll fail) or `time`?
Amarghosh
well you're right about sorting, but the dates are sorted in the XML so for this exemple it will work. The comparison is using date objects, which I think would be the best way
fana