FLEX 时间计算

<?xml version="1.0" encoding="utf-8"?> 

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 

        layout="vertical" 

        verticalAlign="middle" 

        backgroundColor="white" 

        creationComplete="init()"> 

  

    <mx:Script> 

        <![CDATA[ 

            import mx.controls.dataGridClasses.DataGridColumn; 

            private function init():void { 

                var zeroDate:Date = new Date(0); 

  

                /* fullYear */ 

                arrColl.addItem({label:"+2 years", data:dateAdd("fullYear", 2, zeroDate)}); 

                arrColl.addItem({label:"-2 years", data:dateAdd("fullYear", -2, zeroDate)}); 

                /* month */ 

                arrColl.addItem({label:"+11 months", data:dateAdd("month", 11)}); 

                arrColl.addItem({label:"-11 months", data:dateAdd("month", -11)}); 

                /* date */ 

                arrColl.addItem({label:"+4 date", data:dateAdd("date", 4)}); 

                arrColl.addItem({label:"-4 date", data:dateAdd("date", -4)}); 

                /* hours */ 

                arrColl.addItem({label:"+6 hours", data:dateAdd("hours", 6)}); 

                arrColl.addItem({label:"-6 hours", data:dateAdd("hours", -6)}); 

                /* minutes */ 

                arrColl.addItem({label:"+45 minutes", data:dateAdd("minutes", 45)}); 

                arrColl.addItem({label:"-45 minutes", data:dateAdd("minutes", -45)}); 

                /* seconds */ 

                arrColl.addItem({label:"+900 seconds", data:dateAdd("seconds", 900)}); 

                arrColl.addItem({label:"-900 seconds", data:dateAdd("seconds", -900)}); 

                /* milliseconds */ 

                arrColl.addItem({label:"+720000 milliseconds", data:dateAdd("milliseconds", 720000)}); 

                arrColl.addItem({label:"-720000 milliseconds", data:dateAdd("milliseconds", -720000)}); 

            } 

  

            private function dateAdd(datepart:String = "", number:Number = 0, date:Date = null):Date { 

                if (date == null) { 

                    /* Default to current date. */ 

                    date = new Date(); 

                } 

  

                var returnDate:Date = new Date(date.time);; 

  

                switch (datepart.toLowerCase()) { 

                    case "fullyear": 

                    case "month": 

                    case "date": 

                    case "hours": 

                    case "minutes": 

                    case "seconds": 

                    case "milliseconds": 

                        returnDate[datepart] += number; 

                        break; 

                    default: 

                        /* Unknown date part, do nothing. */ 

                        break; 

                } 

                return returnDate; 

            } 

  

            private function data_labelFunc(item:Object, column:DataGridColumn):String { 

                return dateFormatter.format(item[column.dataField]); 

            } 

        ]]> 

    </mx:Script> 

  

    <mx:ArrayCollection id="arrColl" /> 

  

    <mx:DateFormatter id="dateFormatter" 

            formatString="YYYY/MM/DD HH:NN:SS" /> 

  

    <mx:Label text="Now: {dateAdd()}" /> 

    <mx:DataGrid id="dataGrid" 

            dataProvider="{arrColl}" 

            sortableColumns="false" 

            width="400"> 

        <mx:columns> 

            <mx:DataGridColumn dataField="label" 

                    headerText="Label" 

                    width="150" /> 

            <mx:DataGridColumn dataField="data" 

                    headerText="YYYY/MM/DD HH:NN:SS" 

                    labelFunction="data_labelFunc" /> 

        </mx:columns> 

    </mx:DataGrid> 

  

</mx:Application>

returnDate[datepart] += number;

你可能感兴趣的:(Flex)