开发者

comparison of two dates

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


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.


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

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

if( ObjectUtil.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.


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?


private function differenceBetweenDates(date1:Date, date2:Date):Number{
    var MS_PER_DAY:uint = 1000 * 60 * 60 * 24;
    var tempDate:Date = new Date(date2.time - date1.time);
        var difference:Number = 
        Math.abs(Math.round((tempDate.time / MS_PER_DAY)));
    return difference;
}


I have achieved comparing dates succesfully using below code:

//here i have to compare two dates ,these are startdate and enddate.

// gets millisecs counts from 1970 midnight till sellected start date
var Starttimecounts : Number = popJobWin.DFStartDate.selectedDate.time; 

// gets millisecs counts from 1970 midnight till sellected end date
var Endtimecounts : Number = popJobWin.DFEndDate.selectedDate.time ;


if (Starttimecounts > Endtimecounts)
{
    Alert.show('end date should not lesser than start date..wrong!');   
    //replace your logic here                       
}
else
{
    Alert.show('correct!');
    //replace your logic here                       
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜