flex日期合并与拆分
<?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" creationComplete="init(event)" minWidth="955" minHeight="600"> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.FlexEvent; import mx.formatters.DateFormatter; import mx.messaging.AbstractConsumer; import spark.formatters.DateTimeFormatter; private var dateTime_str:String = null; private var dateTime_date:Date = null; protected function init(event:FlexEvent):void { var nowDate:Date = new Date(); this.dateTime.selectedDate = nowDate; this.hour.text = (String)(nowDate.hours); this.minute.text = (String)(nowDate.minutes); } //日期合并 private function combineDate():void { var dateTimeFormatter:DateTimeFormatter = new DateTimeFormatter(); dateTimeFormatter.dateTimePattern = "yyyy-MM-dd"; var nowDate_str:String = dateTimeFormatter.format(this.dateTime.selectedDate); dateTime_str = nowDate_str + " " + this.hour.text + ":" + this.minute.text; dateTime_date = DateFormatter.parseDateString(dateTime_str); //Alert.show(dateTime_date.toString(), "promt"); this.displayDate.text = dateTime_str; } //日期拆分 protected function btnClickHandler(event:MouseEvent):void { this.combineDate(); var str_a:String = this.dateTime_str; var date_a:Date = this.dateTime_date; /* //Method1 var day_a:String = (String)(date_a.fullYear) + "-" + (String)(date_a.month) + "-" + (String)(date_a.date); var hour_a:String = (String)(date_a.hours); var minute_a:String = (String)(date_a.minutes); //Alert.show(str_a, "promt"); //Alert.show(day_a, "day_a"); this.getTime.text = day_a; this.getHour.text = hour_a; this.getMinute.text = minute_a; */ //Method2 this.getTime.text = str_a.substr(0, 10); this.getHour.text = str_a.substr(11, 2); this.getMinute.text = str_a.substr(14,2); } ]]> </fx:Script> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> <s:VGroup> <s:HGroup> <mx:DateField yearNavigationEnabled="true" id="dateTime" width="110" dayNames='["周日","周一","周二","周三","周四","周五","周六"]' monthNames='["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]' formatString="YYYY-MM-DD"/> <s:TextInput id="hour" maxChars="2" restrict="0-9" width="20"/><s:Label text="时"/> <s:TextInput id="minute" maxChars="2" restrict="0-9" width="20"/><s:Label text="分"/> </s:HGroup> <s:HGroup> <s:Label text="显示日期"/><s:TextInput id="displayDate"/> </s:HGroup> <s:HGroup> <s:Button id="btn1" label="Combine" click="combineDate()"/> <s:Button id="btn2" label="Split" click="btnClickHandler(event)"/> </s:HGroup> <s:HGroup> <s:Label text="显示日期"/><s:TextInput id="getTime"/> <s:TextInput id="getHour" width="40"/><s:Label text="时"/> <s:TextInput id="getMinute" width="40"/><s:Label text="分"/> </s:HGroup> </s:VGroup> </s:Application>