Flex 3: compare two dates

submit to reddit

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())

Related posts:

  1. Zend_Date with dates before 1901
  2. Flex 3 DateChooser UTC issue
  3. Download Custom Flex 3 DateChooser

You are a GREAT developer? Click here to answer the weekly quiz!

This entry was posted in flex 3, web development and tagged , , , . Bookmark the permalink.

11 Responses to 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 forgot to put public in front of the function so it should say:

    ….

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

    ….

  3. 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){

  4. pmani says:

    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)

  5. Stoimen says:

    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

  6. onir says:

    I’m not sure I manage this work. Can help please?

  7. self fish says:

    @onir – It’s working man

  8. Tahir Azeem Alvi says:

    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.

  9. Lenin.G says:

    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

  10. Matthew Luke says:

    if( date1.toString() == date2.toString() ) { } to test equality. Use any other operators to make different comparisons

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">