ActionScript Date Comparison

ActionScript Date Comparison

Posted by Brad Wood
Oct 15, 2008 00:29:00 UTC
I was just staring a a piece of ActionScript code trying to figure out why the two date objects I was comparing were not coming up equal. I'm ashamed to say I didn't notice the problem at first because I had mistakenly typed
[code]if(date1 = date2)[/code]
Two equals signs ("==") work a little better than one since the single operator obviously changes the value of the former operand. :) Once I fixed that, I found my real problem.Turns out that ActionScript (like JavaScript) does not like to compare dates directly. It stores them as a number of milliseconds since an epoch date on January 1st, 1970. The .getTime() method of a date object will return you those milliseconds which is what you use to make the comparison.
[code]if(date1.getTime() == date2.getTime())[/code]



You can also do it with mx.utils.ObjectUtil::dateCompare(date, date).

Brad Wood

@Radekg: Thanks. You are always full of good advice. :)

Site Updates

Entry Comments

Entries Search