How can I create an array of dates, one for every day, starting Jan 1st 2008 to Dec 31st 2008 in ActionScript 3?
如何在ActionScript 3中创建2008年1月1日至2008年12月31日期间的每日一个日期数组?
It would be ideal if it worked on any date range...
如果它适用于任何日期范围,那将是理想的...
5 个解决方案
#1
How about this :
这个怎么样 :
function getDateArray(start:Date, end:Date):Array
{
var result:Array = new Array();
var iter:Date = new Date(start); //you could remove or standardise time info here
while (iter <= end)
{
result.push(new Date(iter));
iter.setDate(iter.getDate() + 1);
}
return result;
}
Let the AS3 Date class deal with the actual issue of number of days per month, leap years etc.
让AS3 Date类处理每月天数,闰年等的实际问题。
There may well be a better way to do whatever requires you to have an array of dates, but I'm sure I've used something like this before for legitimate reasons.
可能有更好的方法来做任何需要你有一系列日期的事情,但我确定我之前因为合理的原因使用了这样的东西。
#2
Just wondering why you would need an array of dates. Not saying there probably isn't a good reason for it, but it just seems a bit odd is all. If you have a start and end date then chances are there's probably a better method at doing whatever it is you're trying to do. Any more info?
只是想知道为什么你需要一系列日期。不是说它可能没有充分的理由,但它似乎有点奇怪。如果你有一个开始和结束日期,那么可能有更好的方法来做你正在尝试做的事情。更多信息?
#3
I needed an array of days between 2 dates because my application has a custom hSlider with 2 thumbs which allow the user to change the range of date they wish to bind the data to. (think google finanace) ...
我需要两个日期之间的天数,因为我的应用程序有一个带有2个拇指的自定义hSlider,允许用户更改他们希望将数据绑定到的日期范围。 (想想谷歌财务)...
My solution:
var startDate:Date = Application.application._model.minDate;
var endDate:Date = Application.application._model.maxDate;
var timespan:Number = endDate.getTime() - startDate.getTime();
var aDay:Number = 86400000;
var days:Number = timespan / aDay;
dateData.length = 0;
var i:int = 0;
dateData.push(new Date(startDate));
dateData.push(new Date(endDate));
for(i = 0; i < days; i++){
var newDate:Date = new Date(startDate.getTime() + i * aDay);
dateData.push(newDate);
}
slider.setThumbValueAt(0, Number(dateData[0]));
slider.setThumbValueAt(1, Number(dateData[i]));
#4
Obviously this may not fit in with your application, but for arguments sake if I wanted to have an hSlider that controlled a date value within a given range, I would set the slider up to return a value between 0 and 1 and use that as a percentage of milliseconds between two dates. An example of this would be:
显然这可能不适合你的应用程序,但为了论证,如果我想要一个控制日期值在给定范围内的hSlider,我会设置滑块向上返回0到1之间的值并将其用作两个日期之间的毫秒百分比。一个例子是:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<mx:Script>
<![CDATA[
import mx.events.SliderEvent;
private static var START_DATE : Date = new Date(2009,0);
private static var END_DATE : Date = new Date(2010,0);
[Bindable]
protected var currentDate : Date;
protected function dateSliderChangeHandler(event : SliderEvent) : void
{
var differenceInMilli : Number = END_DATE.time - START_DATE.time;
var percantageMilli : Number = differenceInMilli * event.value;
var currentDateInMilli : Number = START_DATE.time + percantageMilli;
currentDate = new Date(currentDateInMilli);
}
]]>
</mx:Script>
<mx:HSlider id="dateSlider"
width="200"
minimum="0"
maximum="1"
change="dateSliderChangeHandler(event)" />
<mx:Label text="{currentDate}" />
That way i don't have to create an array of all the dates. Obviously if you only needed a certain accuracy in the date then you would need some extra logic to round the dates up or down.
这样我就不必创建所有日期的数组。显然,如果您在日期中只需要一定的准确性,那么您需要一些额外的逻辑来向上或向下舍入日期。
#5
There's nothing fancy to it: just use two for loops, with the inner loop checking the month index ("Thirty days has September, April, June and November") and accounting for leap year (I believe this year is one), remembering ActionScript's Date object doesn't use zero-based indexes for days (though it does for months):
没有什么好看的:只需使用两个for循环,内循环检查月份索引(“三十天有九月,四月,六月和十一月”)并计算闰年(我相信今年是一年),记住ActionScript的日期对象在几天内不使用从零开始的索引(尽管它持续数月):
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="app_init()">
<mx:Script>
<![CDATA[
private function app_init():void
{
var dates:Array = new Array();
for (var month:int = 0; month < 12; month++)
{
for (var day:int = 1; day <= 31; day++)
{
var date:Date = new Date(2008, month, day);
// 30 days has September (8), April (3), June (5) and November (10)...
if ((month == 8 || month == 3 || month == 5 || month == 10) && day == 31)
{
break;
}
// Leap year
else if (month == 1 && day > 29)
{
break;
}
dates.push(date);
}
}
for each (var d:Date in dates)
{
trace(d);
}
}
]]>
</mx:Script>
</mx:Application>
Make sense? (If there's a more elegant way, I'm not aware of it.)
合理? (如果有更优雅的方式,我不知道。)
#1
How about this :
这个怎么样 :
function getDateArray(start:Date, end:Date):Array
{
var result:Array = new Array();
var iter:Date = new Date(start); //you could remove or standardise time info here
while (iter <= end)
{
result.push(new Date(iter));
iter.setDate(iter.getDate() + 1);
}
return result;
}
Let the AS3 Date class deal with the actual issue of number of days per month, leap years etc.
让AS3 Date类处理每月天数,闰年等的实际问题。
There may well be a better way to do whatever requires you to have an array of dates, but I'm sure I've used something like this before for legitimate reasons.
可能有更好的方法来做任何需要你有一系列日期的事情,但我确定我之前因为合理的原因使用了这样的东西。
#2
Just wondering why you would need an array of dates. Not saying there probably isn't a good reason for it, but it just seems a bit odd is all. If you have a start and end date then chances are there's probably a better method at doing whatever it is you're trying to do. Any more info?
只是想知道为什么你需要一系列日期。不是说它可能没有充分的理由,但它似乎有点奇怪。如果你有一个开始和结束日期,那么可能有更好的方法来做你正在尝试做的事情。更多信息?
#3
I needed an array of days between 2 dates because my application has a custom hSlider with 2 thumbs which allow the user to change the range of date they wish to bind the data to. (think google finanace) ...
我需要两个日期之间的天数,因为我的应用程序有一个带有2个拇指的自定义hSlider,允许用户更改他们希望将数据绑定到的日期范围。 (想想谷歌财务)...
My solution:
var startDate:Date = Application.application._model.minDate;
var endDate:Date = Application.application._model.maxDate;
var timespan:Number = endDate.getTime() - startDate.getTime();
var aDay:Number = 86400000;
var days:Number = timespan / aDay;
dateData.length = 0;
var i:int = 0;
dateData.push(new Date(startDate));
dateData.push(new Date(endDate));
for(i = 0; i < days; i++){
var newDate:Date = new Date(startDate.getTime() + i * aDay);
dateData.push(newDate);
}
slider.setThumbValueAt(0, Number(dateData[0]));
slider.setThumbValueAt(1, Number(dateData[i]));
#4
Obviously this may not fit in with your application, but for arguments sake if I wanted to have an hSlider that controlled a date value within a given range, I would set the slider up to return a value between 0 and 1 and use that as a percentage of milliseconds between two dates. An example of this would be:
显然这可能不适合你的应用程序,但为了论证,如果我想要一个控制日期值在给定范围内的hSlider,我会设置滑块向上返回0到1之间的值并将其用作两个日期之间的毫秒百分比。一个例子是:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<mx:Script>
<![CDATA[
import mx.events.SliderEvent;
private static var START_DATE : Date = new Date(2009,0);
private static var END_DATE : Date = new Date(2010,0);
[Bindable]
protected var currentDate : Date;
protected function dateSliderChangeHandler(event : SliderEvent) : void
{
var differenceInMilli : Number = END_DATE.time - START_DATE.time;
var percantageMilli : Number = differenceInMilli * event.value;
var currentDateInMilli : Number = START_DATE.time + percantageMilli;
currentDate = new Date(currentDateInMilli);
}
]]>
</mx:Script>
<mx:HSlider id="dateSlider"
width="200"
minimum="0"
maximum="1"
change="dateSliderChangeHandler(event)" />
<mx:Label text="{currentDate}" />
That way i don't have to create an array of all the dates. Obviously if you only needed a certain accuracy in the date then you would need some extra logic to round the dates up or down.
这样我就不必创建所有日期的数组。显然,如果您在日期中只需要一定的准确性,那么您需要一些额外的逻辑来向上或向下舍入日期。
#5
There's nothing fancy to it: just use two for loops, with the inner loop checking the month index ("Thirty days has September, April, June and November") and accounting for leap year (I believe this year is one), remembering ActionScript's Date object doesn't use zero-based indexes for days (though it does for months):
没有什么好看的:只需使用两个for循环,内循环检查月份索引(“三十天有九月,四月,六月和十一月”)并计算闰年(我相信今年是一年),记住ActionScript的日期对象在几天内不使用从零开始的索引(尽管它持续数月):
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="app_init()">
<mx:Script>
<![CDATA[
private function app_init():void
{
var dates:Array = new Array();
for (var month:int = 0; month < 12; month++)
{
for (var day:int = 1; day <= 31; day++)
{
var date:Date = new Date(2008, month, day);
// 30 days has September (8), April (3), June (5) and November (10)...
if ((month == 8 || month == 3 || month == 5 || month == 10) && day == 31)
{
break;
}
// Leap year
else if (month == 1 && day > 29)
{
break;
}
dates.push(date);
}
}
for each (var d:Date in dates)
{
trace(d);
}
}
]]>
</mx:Script>
</mx:Application>
Make sense? (If there's a more elegant way, I'm not aware of it.)
合理? (如果有更优雅的方式,我不知道。)