flex 动态显示时间
直接上代码。可以当组件用。
<?xml version="1.0" encoding="utf-8"?> <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="278" height="22" creationComplete="init()"> <fx:Declarations> </fx:Declarations> <fx:Script> <![CDATA[ private var _time:Date; //日期 private var timer:Timer; //计数器 public function init():void { this.showCurDate(); timer.addEventListener(TimerEvent.TIMER,timeHandler); timer.start(); } public function showCurDate():void { _time = new Date(); var monStr:String; var dateStr:String; var hoursStr:String; var minutesStr:String; var secondsStr:String; var week:String; var w:Number=_time.day; var seconds:Number = _time.getSeconds(); //秒 var minutes:Number = _time.getMinutes(); //分 var hours:Number = _time.getHours(); //时 var day:Number = _time.getDay(); //星期 var date:Number = _time.getDate(); //日 var month:Number = _time.getMonth()+1; //月 var year:Number = _time.getFullYear(); //年 timer = new Timer(1000); if(w==0){ week="星期天"; } if(w==1){ week="星期一"; } if(w==2){ week="星期二"; } if(w==3){ week="星期三"; } if(w==4){ week="星期四"; } if(w==5){ week="星期五"; } if(w==6){ week="星期六"; } if (month<10) { monStr = "0"+String(month); } else { monStr = String(month); } if (date<10) { dateStr = "0"+String(date); } else { dateStr = String(date); } if (hours<10) { hoursStr = "0"+String(hours); } else { hoursStr = String(hours); } if (minutes<10) { minutesStr = "0"+String(minutes); } else { minutesStr = String(minutes); } if (seconds<10) { secondsStr = "0"+String(seconds); } else { secondsStr = String(seconds); } timelb.text = year+"年"+monStr+"月"+dateStr+"日 "+hoursStr+":"+minutesStr+":"+secondsStr+" "+week; //响应每秒更新一次 } public function timeHandler(evt:TimerEvent):void { this.showCurDate(); } ]]> </fx:Script> <s:Label id="timelb" x="-27" y="1" width="365" color="#FAF6F6" fontSize="16"/> </s:Group>