我正在为大学图书馆开发一个新网站。它将包含一个功能,可显示任何一天的开放时间。许多库使用PHP和后端数据库或Google Calendar Integration的某种组合来实现这一点。出于各种原因,我们无法做到这一点,所以我选择使用Javascript,因为我们知道可以使用我们的CMS(WordPress)而不必打扰校园网站管理员。
我最初的方法是使用几个不同的文件,每个文件都有一个switch语句,根据星期几显示小时数。我们有3种类型的小时(常规,考试周和代祷),只有一个地点。然而,不得不交换文件会引入一定程度的人为错误。
因此我决定使用一个文件。最有意义的方法是获取日期,将其格式化为月份/月份(第一秒,第三等)/日期,并使用一系列if ... else语句将当前日期与然后运行一个switch语句来选出一周当天的正确小时数。
问题是第一个if语句中的条件是否保持评估为真,即使它不是。任何人都可以告诉我我做错了什么吗?或者有更好的方法来完成这项任务吗?
这是我的代码:
http://jsfiddle.net/mjcodelib/9aKmC/
var current = new Date(); //creates new date object
var date = current.getDate(); //gets date of month
var weekDay = current.getDay(); //gets day of week
var week_num = Math.floor((date - 1) / 7) + 1; //determines the week in the month
var month = current.getMonth() + 1; //gets month number
var date_formatted = month + '/' + week_num + '/' + weekDay;
//puts date into month/week number/date format
var date_month = month + '/' + date;
var date_test = new Date (2014, 11, 24); //for troubleshooting
if (date_formatted >= '12/3/1') { //begin winter intercession
switch (weekDay) {
case 0:
document.getElementById("hours").innerHTML="Closed Today";
break;
case 1:
case 2:
case 3:
case 4:
document.getElementById("hours").innerHTML="Today's Hours: 8:00AM to 4:30PM" + date_test;
break;
case 5:
document.getElementById("hours").innerHTML="Today's Hours: 8:00AM to 4:00PM";
break;
case 6:
document.getElementById("hours").innerHTML="Closed Today";
break;
}
} else if (date_formatted >= '12/2/1') { //begin fall exam week
switch (weekDay) {
case 0:
document.getElementById("hours").innerHTML="Today's Hours: 2:00PM to 12:00PM";
break;
case 1:
case 2:
case 3:
case 4:
document.getElementById("hours").innerHTML="Today's Hours: 7:30AM to 12:00AM";
break;
case 5:
document.getElementById("hours").innerHTML="Today's Hours: 7:30AM to 4:00PM";
break;
case 6:
document.getElementById("hours").innerHTML="Closed Today";
break;
}
} else if (date_formatted >= '11/4/1') { //begin Thanksgiving intercession
switch (weekDay) {
case 0:
document.getElementById("hours").innerHTML="Closed Today";
break;
case 1:
case 2:
case 3:
case 4:
document.getElementById("hours").innerHTML="Today's Hours: 8:00AM to 4:30PM";
break;
case 5:
document.getElementById("hours").innerHTML="Today's Hours: 8:00AM to 4:00PM";
break;
case 6:
document.getElementById("hours").innerHTML="Closed Today";
break;
}
} else if (date_formatted >= '10/4/1') { //resume regular hours after fall break
switch (weekDay) {
case 0:
document.getElementByID("hours").innerHTML="Today's Hours: 2:00PM to 10:00PM";
break;
case 1:
case 2:
case 3:
case 4:
document.getElementById("hours").innerHTML="Today's Hours: 7:30AM to 10:00PM";
break;
case 5:
document.getElementById("hours").innerHTML="Today's Hours: 7:30AM to 4:00PM";
break;
case 6:
document.getElementById("hours").innerHTML="Closed Today";
break;
}
} else if (date_formatted > '10/3/3') { //Fall break
switch (weekDay) {
case 0:
document.getElementById("hours").innerHTML="Closed Today";
break;
case 1:
case 2:
case 3:
case 4:
document.getElementById("hours").innerHTML="Today's Hours: 8:00AM to 4:30PM";
break;
case 5:
document.getElementById("hours").innerHTML="Today's Hours: 8:00AM to 4:00PM";
break;
case 6:
document.getElementById("hours").innerHTML="Closed Today";
break;
}
} else if (date_formatted == '10/3/3' ) { //begin fall break
document.getElementByID("hours").innerHTML="Today's Hours: 7:30 AM - 5:00PM";
} else if (date_formatted >= '8/3/1') { //begin fall semester
switch (weekDay) {
case 0:
document.getElementByID("hours").innerHTML="Today's Hours: 2:00PM to 10:00PM";
break;
case 1:
case 2:
case 3:
case 4:
document.getElementById("hours").innerHTML="Today's Hours: 7:30AM to 10:00PM";
break;
case 5:
document.getElementById("hours").innerHTML="Today's Hours: 7:30AM to 4:00PM";
break;
case 6:
document.getElementById("hours").innerHTML="Closed Today";
break;
}
} else if (date_month >= '8/1') { //begin August intercession
switch (weekDay) {
case 0:
document.getElementById("hours").innerHTML="Closed Today";
break;
case 1:
case 2:
case 3:
case 4:
document.getElementById("hours").innerHTML="Today's Hours: 8:00AM to 4:30PM";
break;
case 5:
document.getElementById("hours").innerHTML="Today's Hours: 8:00AM to 4:00PM";
break;
case 6:
document.getElementById("hours").innerHTML="Closed Today";
break;
}
} else if (date_formatted >= '6/1/1') { //begin regular hours for summer
switch (weekDay) {
case 0:
document.getElementByID("hours").innerHTML="Today's Hours: 2:00PM to 10:00PM";
break;
case 1:
case 2:
case 3:
case 4:
document.getElementById("hours").innerHTML="Today's Hours: 7:30AM to 10:00PM";
break;
case 5:
document.getElementById("hours").innerHTML="Today's Hours: 7:30AM to 4:00PM";
break;
case 6:
document.getElementById("hours").innerHTML="Closed Today";
break;
}
} else if (date_formatted >= '5/2/1') { //begin May intercession
switch (weekDay) {
case 0:
document.getElementById("hours").innerHTML="Closed Today";
break;
case 1:
case 2:
case 3:
case 4:
document.getElementById("hours").innerHTML="Today's Hours: 8:00AM to 4:30PM";
break;
case 5:
document.getElementById("hours").innerHTML="Today's Hours: 8:00AM to 4:00PM";
break;
case 6:
document.getElementById("hours").innerHTML="Closed Today";
break;
}
} else if (date_formatted >= '5/1/1') { //begin Spring exam week
switch (weekDay) {
case 0:
document.getElementById("hours").innerHTML="Today's Hours: 2:00PM to 12:00PM";
break;
case 1:
case 2:
case 3:
case 4:
document.getElementById("hours").innerHTML="Today's Hours: 7:30AM to 12:00AM";
break;
case 5:
document.getElementById("hours").innerHTML="Today's Hours: 7:30AM to 4:00PM";
break;
case 6:
document.getElementById("hours").innerHTML="Closed Today";
break;
}
} else if (date_formatted >= '3/3/1') { //resume regular hours after Spring Break
switch (weekDay) {
case 0:
document.getElementByID("hours").innerHTML="Today's Hours: 2:00PM to 10:00PM";
break;
case 1:
case 2:
case 3:
case 4:
document.getElementById("hours").innerHTML="Today's Hours: 7:30AM to 10:00PM";
break;
case 5:
document.getElementById("hours").innerHTML="Today's Hours: 7:30AM to 4:00PM";
break;
case 6:
document.getElementById("hours").innerHTML="Closed Today";
break;
}
} else if (date_formatted >= '3/2/1') { //begin Spring Break
switch (weekDay) {
case 0:
document.getElementById("hours").innerHTML="Closed Today";
break;
case 1:
case 2:
case 3:
case 4:
document.getElementById("hours").innerHTML="Today's Hours: 8:00AM to 4:30PM";
break;
case 5:
document.getElementById("hours").innerHTML="Today's Hours: 8:00AM to 4:00PM";
break;
case 6:
document.getElementById("hours").innerHTML="Closed Today";
break;
}
} else if (date_formatted >= '1/2/1') { //begin Spring semester
switch (weekDay) {
case 0:
document.getElementByID("hours").innerHTML="Today's Hours: 2:00PM to 10:00PM";
break;
case 1:
case 2:
case 3:
case 4:
document.getElementById("hours").innerHTML="Today's Hours: 7:30AM to 10:00PM";
break;
case 5:
document.getElementById("hours").innerHTML="Today's Hours: 7:30AM to 4:00PM";
break;
case 6:
document.getElementById("hours").innerHTML="Closed Today";
break;
}
} else if (date_formatted >= '1/1/4') { //Winter intercession
switch (weekDay) {
case 0:
document.getElementById("hours").innerHTML="Closed Today";
break;
case 1:
case 2:
case 3:
case 4:
document.getElementById("hours").innerHTML="Today's Hours: 8:00AM to 4:30PM";
break;
case 5:
document.getElementById("hours").innerHTML="Today's Hours: 8:00AM to 4:00PM";
break;
case 6:
document.getElementById("hours").innerHTML="Closed Today";
break;
}
} else {
document.getElementByID("hours").innerHTML="<a href='http://www.deltastate.edu/academics/libraries/libraries-hours-of-operation/' target='_blank'>Click here for Library hours</a>";
}
答案 0 :(得分:0)
一张纸条,因为我感觉很懒,并且不想弄清楚month/week_num/weekDay
字符串的日历日期,我只是实施了直接的数字比较,所以它不是最有效(或最容易阅读)
我最终做的就是取代每一个
if (date_formatted >= '##/#/#')
与
if (month >= ## && week_num >= # && weekDay >= #)
我还在jsfiddle中添加了一个div#hours,以便您可以实际看到具有不同测试日期的结果,并将document.getElementById("hours")
转换为变量以便于阅读。