在 AS3 中查找明天和后天

find tomorrow day, and the day after tomorrow in AS3

我有这个代码:

    var currentDate=new Date();
var my_date:Date = new Date();
var month=currentDate.getMonth()+1;
var day=currentDate.getDate();
var day2=currentDate.getDate()+1;
var day3=currentDate.getDate()+2;
var day4=currentDate.getDate()+3;
var day5=currentDate.getDate()+4;
var year=currentDate.getFullYear();
var tomorrow;
var three;
var four;
var five;
var months:Array = ["janvier", "fevrier", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "decembre"];
var today =(day+" "+ months[my_date.month]);
var days:Array = ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"];

我想要明天(以及接下来的 5 个)。

我试过了:

tomorrow =(days[my_date.day+1]+" "+day2 +" "+ months[my_date.month]+" "+ year);
    trace(tomorrow);

它正在工作。我有 "samedi 5 juillet 2015"

但后来我试过了:

 day3 =(days[my_date.day+2]+" "+day2 +" "+ months[my_date.month]+" "+ year);
trace(day3 );

但它的结果是 "undefined 6 juillet 2015"。

你知道我该怎么做吗?

谢谢


编辑

因此,我已将命名日添加到我的代码中:

var index:int = my_date.day + 2
if(index >= days.length)
{
    index -= days.length;
}

所以这是我的代码:

var currentDate=new Date();
var my_date:Date = new Date();
var month=currentDate.getMonth()+1;
var day=currentDate.getDate();
var day2=currentDate.getDate()+1;
var day3=currentDate.getDate()+2;
var year=currentDate.getFullYear();
var tomorrow;
var three;
var four;
var five;
var six;
var months:Array = ["janvier", "fevrier", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "decembre"];
var days:Array = ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"];

var today =(days[my_date.day]+" "+day +" "+ months[my_date.month]+" "+ year);
var index:int = my_date.day + 2
if(index >= days.length)
{
    index -= days.length;
}
trace(days[index+6]);

我为星期天添加了这些行,这是一个错误(星期六是 "undefined"):

if(days[index] == "dimanche"){
 tomorrow =(days[index+6]+" "+day2 +" "+ months[my_date.month]+" "+ year);
}else{
 tomorrow =(days[index-1]+" "+day2 +" "+ months[my_date.month]+" "+ year);
}

然后

three = (days[index] + " " + day3 + " " + months[my_date.month] + " " +  year);
    four = (days[index+1] + " " + day4 + " " + months[my_date.month] + " " +  year);
    five = (days[index+2] + " " + day3 + " " + months[my_date.month] + " " +  year);
}

不过好像不是天天都用。

对于“七月三号”,它运行良好:

但是如果我更改计算机日期并选择,例如,7 月 23 日

有些日子是"undefined"。

知道为什么吗?

问题似乎与索引有关。您正在尝试访问 days[my_date.day+2],它(取决于日期)可能引用了超出范围的索引。如果 my_date.day + 1my_date.day + 2 大于 days.length,您将遇到同样的问题。

你可以用一个简单的条件来解决这个问题。例如,如果 my_date.day + 2 大于或等于 days.length,则访问 days[my_date.day + 2 - days.length]。这确保我们调用的内容在范围内。

示例:

var index:int = my_date.day + 2
if(index >= days.length)
{
    index %= days.length;
}
day3 = (days[index] + " " + day2 + " " + months[my_date.month] + " " +  year);
trace(day3);

这应该可以解决您的问题。希望对您有所帮助!

要使用月份和日期数组显示未来日期,您可以这样做:

var months:Array = ['janvier', 'fevrier', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'decembre'],
    days:Array = ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi'];

function show_7_days(date:Date): void {
    var future_date:Date = new Date(date.fullYear, date.month, date.date);
    for(var i:int = 1; i < 8; i++){
        future_date.date = date.date + i;       
        trace(
            days[future_date.getDay()] + ' ' 
            + future_date.getDate() + ' ' 
            + months[future_date.getMonth()] + ' ' 
            + future_date.getFullYear()
        );
    }
}

然后:

show_7_days(new Date());

// gives : 
//      samedi 4 juillet 2015
//      dimanche 5 juillet 2015
//      lundi 6 juillet 2015
//      mardi 7 juillet 2015
//      mercredi 8 juillet 2015
//      jeudi 9 juillet 2015
//      vendredi 10 juillet 2015

以及特定日期:

show_7_days(new Date(1900, 3, 3));

// gives : 
//      mercredi 4 avril 1900
//      jeudi 5 avril 1900
//      vendredi 6 avril 1900
//      samedi 7 avril 1900
//      dimanche 8 avril 1900
//      lundi 9 avril 1900
//      mardi 10 avril 1900

但您也可以使用 flash.globalization.DateTimeFormatter 来简化事情,如下所示:

var date_formater:DateTimeFormatter = new DateTimeFormatter('fr-FR', DateTimeStyle.LONG, DateTimeStyle.NONE);

function show_7_days(date:Date): void {
    var future_date:Date = new Date(date.fullYear, date.month, date.date);
    for(var i:int = 1; i < 8; i++){
        future_date.date = date.date + i;       
        trace(date_formater.format(future_date));
    }
}

这会给你相同的结果。

希望能帮到你。