flex中日期的格式化

时间:2023-03-08 21:46:50
flex中日期的格式化

今天我做的项目中需要把时间给拆分了,形式为:yyyy-MM-DD HH mm,

下面是我的代码实现:

<?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"
               width="100%" height="100%"
               >
    <fx:Declarations>
        <mx:DateFormatter id="dateFormat" formatString="YYYY-MM-DD JJ:NN:SS"/>
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
        import mx.formatters.*;
        [Bindable]
        private var time:Date=new Date();  
        private function TestDateTimeToString():void
        {
        var fr:DateFormatter=new DateFormatter();
        var fs:DateFormatter=new DateFormatter();
        var ft:DateFormatter=new DateFormatter();
        fr.formatString="YYYY-MM-DD JJ:NN:SS";
        fs.formatString="JJ";
        ft.formatString="NN";
        myDate.text=fr.format(time);
        hour.text=fs.format(time);
        minutes.text=ft.format(time);
        }
        ]]>   
    </fx:Script>
    <mx:Panel width="100%" height="100%">
        <mx:Label id="currentTimeText" text="当前日期:"/>
        <mx:Label id="currentTime" text="{dateFormat.format(time)}"/>
        <mx:Button label="转换日期" click="TestDateTimeToString()"/>
        <mx:Label id="myDate" text="时间还没有被转换" />
        <mx:Label id="hour" text="时间还没有被转换"/>
        <mx:Label id="minutes" text="时间还没有被转换"/>
    </mx:Panel>
</s:Application>
效果如下:

flex中日期的格式化