新建一个Datefield.mxml文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<mx:DateFormatter id="dateformat" formatString="YYYY-MM-DD"/>
</fx:Declarations>
<fx:Style>
global{
fontSize:16pt;
}
.subDateChooser {
backgroundColor:#df7cf8;
color: black;
headerColor:yellow;
}
</fx:Style>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.CalendarLayoutChangeEvent;
import Parameter; //引用参数类
private static const MILISECONDS:int=1000;
private static const SECONDS:int=60;
private static const MINUTES:int=60;
private static const HOURS:int=24;
private function update(event:CalendarLayoutChangeEvent):void{
try{
//结束日期-开始日期=合计天数
var diff:Number=endDate.selectedDate.getTime()-startDate.selectedDate.getTime();
var days:int= int(diff/(MILISECONDS*SECONDS*MINUTES*HOURS));
duration.text=days.toString();
this.startDate.text=dateformat.format(this.startDate.text);
this.endDate.text=dateformat.format(this.endDate.text);
var current:Date = new Date( );
}
catch(ex:Error){
}
}
private function formatDate(date:Date):String {
return dateformat.format(date);
}
]]>
</fx:Script>
<mx:Canvas backgroundColor="#F0F8E3" horizontalCenter="0" verticalCenter="0" width="284" height="220">
<mx:DateField x="111" y="50" id="startDate" change="update(event)" labelFunction="formatDate" dateChooserStyleName="subDateChooser" monthNames='{Parameter.MONTH_NAMES}' dayNames="{Parameter.DAY_NAMES}" />
<mx:Label x="19" y="52" text="起始日期"/>
<mx:Label x="19" y="111" text="结束日期"/>
<mx:DateField id="endDate" dateChooserStyleName="subDateChooser" labelFunction="formatDate" yearNavigationEnabled="true"
change="update(event)" x="111" y="109">
<mx:monthNames>{Parameter.MONTH_NAMES}</mx:monthNames>
<mx:dayNames>{Parameter.DAY_NAMES}</mx:dayNames>
</mx:DateField>
<mx:Label x="19" y="165" text="合计天数"/>
<mx:TextInput x="111" y="163" id="duration" width="93"/>
<mx:Label x="77" y="3" text="旅行天数计算"/>
</mx:Canvas>
</s:Application>
4.在同一目录下,新建一个ActionScript类文件。名为Parameter。内容如下:DateParameter,代码如下
package
{
public class DateParameter
{
//月份中文
public static const MONTH_NAMES:Array = ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"];
//星期中文
public static const DAY_NAMES:Array = ["七","一","二","三","四","五","六"];
}
}
http://blog.163.com/liwei3324@126/blog/static/47452426201041114826996/