FLEX中日期时间计算的例子

日期计算在很多开发语言中都是一个让人“讨厌”的问题,接下来的例子演示了日期时间相关的一些计算,相信比较符合一些“懒人”的胃口。 :)
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整实现代码(或点击这里察看):

Download: main.mxml
<?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>

代码:Peter deHaan 翻译/整理/编译:minidxer

你可能感兴趣的:(xml,Flex)