I have some code which builds an array of date ranges. I then call a function (from the jquery UI datepicker), passing it a date, and compare that date with dates in the array. I'm doing it this way because the dates are stored in a cms and this is the only way I can output them.
我有一些代码可以构建一个日期范围数组。然后我调用一个函数(来自jquery UI datepicker),传递一个日期,并将该日期与数组中的日期进行比较。我这样做是因为日期存储在cms中,这是我输出它们的唯一方法。
Unfortunately my code only checks the first date range in the array - and I can't figure out why! I think it's probably something simple (/stupid!) - if anyone can shed some light on it I'd be extremely grateful!
不幸的是我的代码只检查数组中的第一个日期范围 - 我无法弄清楚原因!我认为它可能很简单(/愚蠢!) - 如果有人能够对它有所了解,我将非常感激!
The code is below - the june-september range (ps1-pe1) works fine, the december to jan is totally ignored...
代码如下 - 六月至九月范围(ps1-pe1)工作正常,十二月到一月完全被忽略了......
<script type="text/javascript" language="javascript">
var ps1 = new Date(2010, 06-1, 18); // range1 start
var pe1 = new Date(2010, 09-1, 03); // range1 end
var ps2 = new Date(2010, 12-1, 20); // range2 start
var pe2 = new Date(2011, 01-1, 02); // range2 end
var peakStart = new Array(ps1,ps2);
var peakEnd = new Array(pe1,pe2);
function checkDay(date) {
var day = date.getDay();
for (var i=0; i<peakStart.length; i++) {
if ((date > peakStart[i]) && (date < peakEnd[i])) {
return [(day == 5), ''];
} else {
return [(day == 1 || day == 5), ''];
}
}
}
</script>
2 个解决方案
#1
2
Yaggo is quite right, but apparently too terse.
Yaggo是对的,但显然太简洁了。
You want to move the second return statement outside of the loop.
您希望将第二个return语句移到循环之外。
function checkDay(date) {
var day = date.getDay();
for (var i=0; i<peakStart.length; i++) {
if ((date > peakStart[i]) && (date < peakEnd[i])) {
return [(day == 5), ''];
}
}
// it's not during a peak period
return [(day == 1 || day == 5), ''];
}
#2
2
You always call return in the first iteration of for loop.
你总是在for循环的第一次迭代中调用return。
#1
2
Yaggo is quite right, but apparently too terse.
Yaggo是对的,但显然太简洁了。
You want to move the second return statement outside of the loop.
您希望将第二个return语句移到循环之外。
function checkDay(date) {
var day = date.getDay();
for (var i=0; i<peakStart.length; i++) {
if ((date > peakStart[i]) && (date < peakEnd[i])) {
return [(day == 5), ''];
}
}
// it's not during a peak period
return [(day == 1 || day == 5), ''];
}
#2
2
You always call return in the first iteration of for loop.
你总是在for循环的第一次迭代中调用return。