我有这样的代码:
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 );但其结果却是“2015年未确定的6只朱丽叶”。
你知道我怎么能做到吗?
谢谢
编辑
因此,我在我的代码中添加了名字日期的代码:
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]);我已经添加了这些用于“星期日有错误”的行(星期六是“未定义的”):
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日为例

有些日子是“未定的”。
知道为什么吗?
发布于 2015-07-03 12:03:06
要使用月份和天数组显示未来日期,可以这样做:
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));
}
}会给你同样的结果。
希望能帮上忙。
发布于 2015-07-03 01:23:31
这个问题似乎与指数有关。您正在尝试访问days[my_date.day+2],它(取决于日期)可能指超出界限的索引。如果my_date.day + 1或my_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);这应该能解决你的问题。希望能帮上忙!
https://stackoverflow.com/questions/31196877
复制相似问题