Flex 3: compare two dates

Theory of Operation

You’re using Flex 3 and want to compare two dates. The format of the dates is string something like “2009 May 05”. The question is …

What’s the best way to compare them

Well if you’ve the dates as strings and you can easily conver them to something like unix timestamps. If they are a Date object you can try lik so:

date1 = new Date(‘2009’);

date2 = new Date(‘1990’);

if (date1.setTime() < date2.setTime())

13 thoughts on “Flex 3: compare two dates

  1. To compare dates use the ObjectUtil class.

    import mx.utils.ObjectUtil;
    import mx.controls.Alert;
    public var test1:Date = new Date(7/26/09);
    public var test2:Date = new Date(7/21/08);

    function compareDates(date1:Date, date2:Date):void{

    if(ObjectUtil.dateCompare(date1, date2) > -1){

    Alert.show(date2 + ” is before ” + date1);

    } else if (ObjectUtil.dateCompare(date1, date2) = 0){
    Alert.show(date1 + ” ” + date2 + ” are equal”);
    } else{
    Alert.show(date1 + ” is before ” + date2);
    }
    }
    compareDates(test1, test2);

    //the dateCompare method returns 1 if date1 is before //date2 or date 1 is null, 0 if both values are null or //equal, -1 if date2 is before date1 or date2 is null

  2. I always mess up code :). The first if statement should be with the comparison changed and likewise the 2nd one:

    if(ObjectUtil.dateCompare(date1, date2) == -1){

    else if (ObjectUtil.dateCompare(date1, date2) == 0){

  3. i am new for flex ,can u help me?
    i want to find the number of dates from one date to another except saturday and sunday(ex:number of dates is 7 means ,today is starting date(11/30/2009) the end date should be 12/8/2009 (exclude sundays and saturday)

  4. Hi,

    I’m not sure you can achieve this with some built in functionality of Flex. You’d better make a method to do that. It should check for weekends and exclude them, so when you pass two dates it can check the number of week days.

    greetings,
    stoimen

  5. Hi,

    i have a problem face from last day.

    The problem is that i want to run a countdown Timer. The timer is start from two dates
    1. current date (as New Date()).
    2. the immediate Monday (as new Date(“Mon Jan 17 2011”))

    The problem is with 2nd date.

    How i dynamically find the next Monday date ?.

    Thanks and welcome for Help.

  6. i am new in flex..can u help me… how to compare two dates… i written function in to date of date field….anyone can suggest for my problem

  7. Hi Mathew,

    if( date1.toString() == date2.toString() ) { } this code is not working…

    I have written like the below.. but it always giving date1 and date 2 are equal…

    public function compare (event:MouseEvent) : void {

    var date1:Date=new Date(“02 AUG 2012”);
    var date2:Date=new Date(“01 AUG 2012”);

    if(date1.toString()==date2.toString())
    {
    Alert.show(“date1 is equal to date2”);
    }
    else if(date1.toString()<date2.toString())
    {
    Alert.show("Date1 is less than date2");
    }
    else
    {
    Alert.show("Date1 greater than date2");
    }
    }

  8. Maybe the next solution is the right one:
    import mx.controls.DateField;
    —–

    var date1 :Date= DateField.stringToDate('2009',"YYYY");	
    var date2 :Date= DateField.stringToDate('1990',"YYYY");	
    if (date1.getTime() < date2.getTime())

    —-
    First of all, you have to convert correctly string to date;
    Second, getTime(), Stoimene, not setTime()

Leave a Reply

Your email address will not be published. Required fields are marked *