aodz

How to compare 2 dates in ActionScript

Posted on: May 25, 2009


Date objects don’t have a built-in compare() method, but comparing dates  easy.Don’t  look at the Date objects , but rather the values represented by the objects using the Date.getTime() method:

Date.getTime() : Returns the number of milliseconds since midnight January 1, 1970, universal time, for a Date object. Use this method to represent a specific instant in time when comparing two or more Date objects.

This makes comparing dates as trivial as comparing numbers. Here’s a simple method that compares two dates, returning minus one (-1) if the first date is before the second, zero (0) if the dates are equal, or one (1) if the first date is after the second:

public function compare (date1 : Date, date2 : Date) : Number {
var date1Timestamp : Number = date1.getTime ();
var date2Timestamp : Number = date2.getTime ();
var result : Number = -1;
if (date1Timestamp == date2Timestamp){
result = 0;
} else if (date1Timestamp > date2Timestamp){
result = 1;
}
return result;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

I have moved to a different location

check out my new home Flexout

calendar

May 2009
M T W T F S S
« Mar    
 123
45678910
11121314151617
18192021222324
25262728293031

Blog Stat

  • 83,703 Hop's so far!!!

follow me

Archives

Linkedin Blogger Twitter Youtube Orkut

Top Clicks

  • None

latest flickr photos

top rated

%d bloggers like this: