Adobe Air中两个日期之间的日期差异

时间:2012-02-07 12:23:58

标签: actionscript-3 flex air flex4.5

如何使用Flex Builder 4.5通过Adobe Air Desktop应用程序中的DateChooser组件比较用户选择的两个日期之间的天(int)差异?

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 个答案:

答案 0 :(得分:1)

<?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 :(得分:1)

flex中的日期数学支持很差,看起来非常不准确。 Flex可能是一种语言,但为了后人和任何可能存在的遗留代码,请不要使用user1196374接受的答案。他们的答案很粗糙,非常不准确。

下面我已经包含了我为实现既定目标而开发的代码,该代码最初在本文中使用了他的代码作为起点。

免责声明,我不知道这是否涵盖了上一个功能中的每个缺陷,也不知道它是否能很好地处理其他日历。然而,它比接受的答案要好得多。

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;
}