flash actionscript:
Hi, I'm trying to compare two dates that are the same, and perform a specific action if they are the same. Here's my code: When they're equal Flash says that they aren't. Why? Is this a bug b/c it makes no sense! >:( var d = new Date(2004, 3, 17); var e = new Date(2004, 3, 17); trace(d<e); trace(e<d); if (d == e) { trace("equal"); } else { trace("not equal"); }
This is not a bug. Flash is functioning properly. The problem is that == is not designed to compare two date objects. In the case of objects, flash compares "by reference", which means that if they aren't actually pointing to the same exact object in memory, then it will return false. What you can do instead is convert the dates to strings and then use ==, because == will compare strings fine. Alternately, you could compare each individual part of the date individually as integers. IM me if you want any more explanation.
This is not a bug. Flash is functioning properly. The problem is that == is not designed to compare two date objects. In the case of objects, flash compares "by reference", which means that if they aren't actually pointing to the same exact object in memory, then it will return false. What you can do instead is convert the dates to strings and then use ==, because == will compare strings fine. Alternately, you could compare each individual part of the date individually as integers. IM me if you want any more explanation.
split the date objects to arrays and compare the arrays, d = new Date(2004, 3, 17); d1 = d.toString(); d2 = d1.split(""); trace(d2); e = new Date(2004, 4, 17); e1 = e.toString(); e2 = e1.split(""); trace(e2); for(var iv=0; iv!=d2.length; iv++){ if(d2[iv] == e2[iv]){ trace(d2[iv]+" equals "+e2[iv]); } else { trace(d2[iv]+" not equal to "+e2[iv]); } } hth
Don't see what you're looking for? Try a search.
|