我试图找到过去几天的名字。我正在使用此脚本成功获取过去 7 天的名称,但是,如果我增加过去的天数,例如 7 天前、8 天前或更长时间,它将不起作用。
var dayOfWeek = new Array();
dayOfWeek[0] = "Sun";
dayOfWeek[1] = "Mon";
dayOfWeek[2] = "Tue";
dayOfWeek[3] = "Wed";
dayOfWeek[4] = "Thu";
dayOfWeek[5] = "Fri";
dayOfWeek[6] = "Sat";
var myDate = new Date();
myDate.setDate(myDate.getDate());
var myDate1 = new Date();
myDate1.setDate(myDate1.getDate() - 1);
var myDate2 = new Date();
myDate2.setDate(myDate2.getDate() - 2);
var myDate3 = new Date();
myDate3.setDate(myDate3.getDate() - 3);
var myDate4 = new Date();
myDate4.setDate(myDate4.getDate() - 4);
var myDate5 = new Date();
myDate5.setDate(myDate5.getDate() - 5);
var myDate6 = new Date();
myDate6.setDate(myDate6.getDate() - 6);
var on1 = dayOfWeek[myDate.getDay()];
var on2 = dayOfWeek[myDate1.getDay()];
var on3 = dayOfWeek[myDate2.getDay()];
var on4 = dayOfWeek[myDate3.getDay()];
var on5 = dayOfWeek[myDate4.getDay()];
var on6 = dayOfWeek[myDate5.getDay()];
document.write(on5);
这样做的正确方法是什么?