views:

43

answers:

3

hi, how to compare values of 2 dates using actionscript i executed this code in my program..

var time1:Date = new Date(Number(fromDate.substr(0,4)),Number(fromDate.substring(5,7))-1,    Number(fromDate.substring(8,10)));
var time2:Date = new Date(Number(toDate.substr(0,4)),Number(toDate.substring(5,7))-1, Number(toDate.substring(8,10)));

if(time1.getTime() > time2.getTime())
{ 
    Alert.show(time1 + ” is after ” + time2); 
}

im getting error: Error: Unexpected end of token stream

A: 

If you have two dates as Date objects already, just compare them. e.g. a.getTime() > b.getTime().

If they are strings, see their format is acceptable by the default Date.parse() function. If not, you may have other work to do.

Let's see your values first, shall we?

nil
i executed this code in my program..var time1:Date = new Date(Number(fromDate.substr(0,4)),Number(fromDate.substring(5,7))-1, Number(fromDate.substring(8,10))); var time2:Date = new Date(Number(toDate.substr(0,4)),Number(toDate.substring(5,7))-1, Number(toDate.substring(8,10))); if(time1.getTime()>time2.getTime()){ Alert.show(time1 + ” is after ” + time2); }im getting error: Error: Unexpected end of token stream.
This error is due to your source file, no matter it's MXML or ActionScript file, has invalid characters. The quotation marks of ” is after ” are invalid.
nil
+1  A: 

AS3 doesn't support a time delta class like Python so this can actually be a little tricky. There are lots of things to be worried about when comparing dates:

  • daylight savings time (when the clocks change one hour in certain countries Spring and Fall)
  • time-zones
  • leap-years

The roughest way to do things is just to use the time property of a date object. This way you can get an accurate difference between two dates expressed in milliseconds:

var date1:Date = new Date(2001, 9, 12); // Oct. 12, 2001
var date2:Date = new Date(2010, 5, 22); // Jun. 22, 2010
var differenceInMilliseconds:Number = date2.time - date1.time;

Using this time property you can do things like check if one date is before or after another date. You can also do rough calculations on the distance between two dates by defining some constants:

const MILLISECOND_PER_SECOND:int = 1000;
const SECOND_PER_MINUTES:int = 60;
const MINUTES_PER_HOUR:int = 60;
const HOURS_PER_DAY:int = 24;
// ... etc ...

var differenceInSeconds:Number = differenceInMilliseconds / MILLISECOND_PER_SECOND;
var differenceInMinutes:Number = differenceInSeconds / SECOND_PER_MINUTES;
var differenceInHouse:Number = differenceInMinutes / MINUTES_PER_HOUR;
var differenceInDays:Number = differenceInHouse / HOURS_PER_DAY;

Once you get to the level of days you could get problems with daylight savings time since the change of 1 hour can make it seem like a full day has passed when it really hasn't. After days and into weeks or months you run into leap year problems.

James Fassett
A: 

Assuming your string processing code correctly gives you valid date objects, just use the ObjectUtils.dateCompare function to compare 2 dates:

http://livedocs.adobe.com/flex/3/langref/mx/utils/ObjectUtil.html#dateCompare%28%29

if(ObjectUtils.dateCompare(date1,date2 ) == 1){ }

I'm pretty sure that the return types defined in the ASDocs are wrong.

It'll actually return -1 if a is null or before b; 1 if b is null or before.

www.Flextras.com