-2

试图输出日期加上 30 天。我今天加载了脚本,当天显示“未定义”,脚本的其余部分都可以。'未定义,2013 年 4 月 7 日'。

这是我的脚本:

// handle the due date
var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday");
var monthNames = new Array("January","February","March","April","May","June","July", "August","September","October","November","December");    
var todayPlus30 = new Date();
todayPlus30.setDate(todayPlus30.getDate()+30)
var dateStr = (dayNames[todayPlus30.getDate()] + ", " + monthNames[todayPlus30.getMonth()] + " " +  todayPlus30.getDate()  + ", " +  todayPlus30.getFullYear());

$('#date').html(dateStr);
4

2 回答 2

1

您的 dayNames 数组有 7 个元素,因此它有来自 dayNames[0] .. dayNames[6] 的元素。

当你在这里引用它时:

dayNames[todayPlus30.getDate()]

您正在尝试访问未定义的 dayNames[7]。这是因为 todayPlus30.getDate() 返回 7 (getDate() “返回月份中的日期”(根据 MDN)。

将其更改为:

dayNames[todayPlus30.getDay()]

获取星期几。

于 2013-03-08T09:12:27.257 回答
0

对于一周中的(整数)天,您想要getDay,而不是getDate

于 2013-03-08T09:11:50.023 回答