ActionScript Date Comparison

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

if(date1 = date2)

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.

if(date1.getTime() == date2.getTime())

Comments
radekg's Gravatar You can also do it with mx.utils.ObjectUtil::dateCompare(date, date).
# Posted By radekg | 10/15/08 1:58 AM
Brad Wood's Gravatar @Radekg: Thanks. You are always full of good advice. :)
# Posted By Brad Wood | 10/15/08 2:37 AM
BlogCFC (5.9.004) by Ray Camden. Blog Owner: Brad Wood