0
votes

How to compare the days(int) difference between two dates chosen by user, via DateChooser component in Adobe Air Desktop application using Flex builder 4.5 ?

import mx.controls.DateChooser;
import mx.controls.Text;
private var date1:String;
date1 = date_from.text; //giving error 1120:Access of undefined property date_form
2

2 Answers

1
votes
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
    <![CDATA[
        import flash.utils.getTimer;
        import mx.events.CalendarLayoutChangeEvent;
        import mx.events.FlexEvent;
        protected function   
      datechooser_changeHandler(event:CalendarLayoutChangeEvent):void
        {
            var dateDiff:Number
            dateDiff=new Date(dChs1.selectedDate).time-new 
      Date(dChs2.selectedDate).time
            trace (Math.round((dateDiff/86400000)).toString())
        }
        protected function dChs_initializeHandler(event:FlexEvent):void
        {
            var today:Date=new Date()
            event.target.selectedDate=today
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:layout>
    <s:VerticalLayout/>
</s:layout>
<mx:DateChooser id="dChs1" change="datechooser_changeHandler(event)"
                initialize="dChs_initializeHandler(event)">

</mx:DateChooser>
<mx:DateChooser id="dChs2" change="datechooser_changeHandler(event)"
                initialize="dChs_initializeHandler(event)">

</mx:DateChooser>
     </s:WindowedApplication>
1
votes

Date math in flex is poorly supported, and very inaccurate it seems. Flex might be a language on the way out but for posterity sake and any legacy code which might hang around, please do not use the accepted answer by user1196374. Their answer is rough and very inaccurate.

Below I have included code I developed to perform the stated goal, which had initially used his code in this post as a starting point.

Disclaimer, I don't know if this covers every flaw in the previous function, nor do I know if it will handle other calendars well. It is however a vastly better solution than the accepted answer.

public static function dateDiff(dateA:Date, dateB:Date): int{
  var retVal:int = 0;
        if(dateA != null && dateB!=null) {
          retVal = roughDateDiff(toMidnightFromUTCDate(dateA),toMidnightFromUTCDate(dateB));

        }
  return retVal;
}

private static function roughDateDiff(dateA:Date, dateB:Date): int{
  var retVal:int = 0;
  if(dateA != null && dateB!=null){
    if(dateA.time > dateB.time){
      var dateDiff:Number = dateA.time - dateB.time;
      retVal = Math.floor((dateDiff/86400000));
    }else{
      var dateDiff:Number = dateB.time - dateA.time;
      retVal = Math.floor((dateDiff/86400000));
    }
  }
  return retVal;
}

private static function toMidnightFromUTCDate(pre:Date):Date{
  var post:Date = new Date(pre.toUTCString());

  post.setUTCHours(0);
  post.setUTCMinutes(0);
  post.setUTCSeconds(0);
  post.setUTCMilliseconds(0);

  return post;
}