monthDays = 31;
dayOfMonth = 9;
weekOfMonth = 2;

startDay = weekStartingDate (weekOfMonth); // function return 8

// startDay could be 8, 15, 22 or 28
for (var day = startDay; day < (startDay+7) ; day++)
{
     //stuff
}


问题是,当startDay为29+时,(startDay+7)超出了monthDays

考虑周日范围,我想遍历几天。

最佳答案

您应该为for循环定义极限值

for (var day = startDay; day < ((startDay+7) > monthDays ? monthDays : (startDay+7)) ; day++)
{
     //stuff
}

10-05 20:52