AS3 Date Class – correctly parsing to UTC milliseconds

The other day I was trying to parse a date value via Date.parse(date:String) but I kept getting an odd negative number as a result. I didn’t realize what was going on until I checked what the return type of Date.parse was and saw that it was “Number” instead of “int”. Oops. After changing all my millisecond variables from int to Number, everything worked fine. Apparently int doesn’t allocate enough memory – I should have known this.

Lesson learned: Use variables of type Number when trying to get the UTC milliseconds from Date.parse()

var myDate : Date = new Date();
var myInt : int = Date.parse(myDate);
trace(myInt); // Incorrect: -1333562360
var myNum : Number = Date.parse(myDate);
trace(myNum); // Correct: 1222732117000