flex日期的计算
例如:2009年3月——2010年3月
如何将这个时间段里面的所有月份保存到一个数组中?
[解决办法]
代码如下:
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"> <mx:Script> <![CDATA[ import mx.controls.Alert; import mx.formatters.DateFormatter; public function obtainIntervalMonths(startYM:String, endYM:String):Array { if (!startYM || startYM.length != 6 || !endYM || endYM.length != 6) { return null; } var dateFormart:DateFormatter = new DateFormatter(); dateFormart.formatString = "YYYYMM"; var startDate:Date = new Date(Number(startYM.substring(0,4)),Number(startYM.substring(4,6))-1); var currentYM:String = dateFormart.format(startDate); var result:Array = []; while (currentYM <= endYM) { result.push(currentYM); startDate.setMonth(startDate.getMonth()+1,1); currentYM = dateFormart.format(startDate); } return result; } private function print():void { var result:Array = obtainIntervalMonths("200903","201003"); Alert.show(result.toString()); } ]]> </mx:Script> <mx:Button click="print()"/></mx:Application>