日期比较脚本 - 任何问题

时间:2014-07-21 17:08:25

标签: javascript date

以下是我上次发布的日期检查脚本的更新版本。我想到使用函数查看关键日期是否在某个范围内,然后根据这些结果分配小时数。我已成功完成此操作并通过手动设置分配给变量current的日期进行错误检查。但它给了我足够的麻烦,我希望更有经验的眼睛看一看,看看是否有任何我错过的挥之不去的问题。

该脚本的工作原理如下。获取当前日期和星期几后,库的小时数将分配给一组数组。随后的变量集是我们的计划变更的日期。使每一个成为日期对象(而不是数字)似乎是比较苹果与苹果而不会得到错误结果的唯一方法。此外,它还使非程序员更容易更新脚本。在随后的函数中,将当前日期与这些函数进行比较,如果它落在该范围内,则函数返回true。 short_hours函数是必须存在的例外,因为我们在上午7:30到下午5点开放时有一些日子。最后,使用一组if语句调用函数,并使用true来设置函数从一个数组中设置当前小时数。我的代码如下。火焰离开!

http://jsfiddle.net/mjcodelib/XZF59/

//Code written by Michael Paulmeno

var current = new Date();                           //creates new date object
var weekDay = current.getDay();                     //gets day of week (0-6 in Javascript)
var hours = document.getElementById("hours");       //stores code to simplify if statements

//The variables below set the library's operating hours 

var hours_regular = ["Sunday's Hours: 2:00PM to 10:00PM", "Monday's Hours: 7:30AM to 10:00PM", "Tuesday's Hours: 7:30AM to 10:00PM", "Wednesday's Hours: 7:30AM to 10:00PM","Thursday's Hours: 7:30AM to 10:00PM", "Friday's Hours: 7:30AM to 4:00PM", "Closed Today"];  
var hours_exam = ["Sunday's Hours: 2:00PM to 12:00PM", "Monday's Hours: 7:30AM to 12:00PM", "Tuesday's Hours: 7:30AM to 12:00PM", "Wednesday's Hours: 7:30AM to 12:00PM", "Thursday's Hours: 7:30AM to 12:00PM", "Friday's Hours: 7:30AM to 4:00PM", "Closed Today"];
var hours_intercession = ["Closed Today", "Monday's Hours: 8:00AM to 4:30PM", "Tuesday's Hours: 8:00AM to 4:30PM", "Wednesday's Hours: 8:00AM to 4:30PM", "Thursday's Hours: 8:00AM to 4:30PM", "Friday's Hours: 8:00AM to 4:00PM", "Closed Today", "Today's Hours: 8am - 5pm"];
var hours_august_intercession = ["Closed Today", "Monday's Hours: 8:00AM to 5:00PM", "Tuesday's Hours: 8:00AM to 5:00PM", "Wednesday's Hours: 8:00AM to 5:00PM", "Thursday's Hours: 8:00AM to 5:00PM", "Friday's Hours: 8:00AM to 4:00PM", "Closed Today"];
var hours_shortened = "Today's Hours: 7:30AM to 5:00PM";


//These are the 'important dates' for the year.  At each of these dates our operating hours change.  The format is year + month + date. 
//Change the values in parenthesis every year to reflect the important dates for the next academic year.

//Fall Dates
var fall_semester_begin = new Date (2014,7,18);
var fall_break_begin = new Date (2014,9,15);
var fall_break_end = new Date (2014,9,20);
var fall_exams = new Date (2014,11,8);
var thanksgiving_break = new Date (2014,10,24);
var thanksgiving_break_end = new Date (2014,11,1);
var winter_intercession = new Date (2014,11,14);
var winter_holidays = new Date (2015,11,23);

//Spring Dates
var winter_intercession2 = new Date (2015,0,1);
var spring_semester_begin = new Date (2015,0,12);
var spring_break_begin = new Date (2015,2,9);
var spring_break_end = new Date (2015,2,16);
var spring_exams = new Date (2015,4,4);

//Summer Dates
var may_intercession = new Date (2014,4,11);
var summer_hours_regular = new Date(2014,5,2);
var august_intercession = new Date (2014,6,31);

//The functions below compare the current date as formatted above to the important dates to determine at what point in the year the current date falls in.

function regular_hours_spring (today) {
    if (today >= spring_break_end   && today < spring_exams ||      
        today >= spring_semester_begin && today < spring_break_begin ) 
        {       
        return true; }
    else {
        return false;
    }
}

function regular_hours_summer (today){
    if (today >= summer_hours_regular &&
        today < august_intercession) 
        {
        return true;}
    else {return false;}
}

function regular_hours_fall (today) {
    if (today >= thanksgiving_break_end && today < fall_exams ||
        today >= fall_break_end && today < thanksgiving_break ||
        today >= fall_semester_begin && today < fall_break_begin) 
        {       
        return true; }
    else {
        return false;
    }
}

function exam_hours (today) {
    if (today >= fall_exams && today <= winter_intercession ||
        today >= spring_exams && today <= may_intercession) 
        {       
        return true; }
    else {
        return false;
    }
}

function intercession_hours (today) {
    if (today >= winter_intercession && today < winter_holidays         ||
        today >= thanksgiving_break && today < thanksgiving_break_end   ||
        today > fall_break_begin && today < fall_break_end              ||
        today >= august_intercession && today < fall_semester_begin     ||
        today >= may_intercession   && today < summer_hours_regular     ||
        today > spring_break_begin && today < spring_break_end          ||
        today > winter_intercession2 && today < spring_semester_begin) 
        {       
        return true; }
    else {
        return false;
    }
}

function short_hours (today) {
    if (fall_break_begin && today > fall_semester_begin) 
        {       
        return true; }
    else {
        return false; }
}

function closed (today) {
    if (today >= winter_holidays) 
        {
        return true;
        }
    else {
        return false; }
}

//The value returned by the functions above is logged to the error console for troubleshooting

console.log (regular_hours_spring(current));
console.log (regular_hours_summer(current));
console.log (regular_hours_fall(current));
console.log (exam_hours(current));
console.log (intercession_hours(current));
console.log (short_hours(current));
console.log (closed(current));


//The if statements below call the functions above and determine whether the hours displayed should be regular, exam, or intercession.

if (closed (current) && current >= winter_holidays) {
    hours.innerHTML = "Closed Today";
} 
else if(intercession_hours(current) && current >= august_intercession && current < fall_semester_begin) {
        hours.innerHTML = hours_august_intercession[weekDay];
}
else if(intercession_hours(current)) {
    hours.innerHTML = hours_intercession[weekDay];
}
else if (exam_hours (current)) {
    hours.innerHTML = hours_exam[weekDay];
} 
else if(regular_hours_spring(current)){
    hours.innerHTML = hours_intercession[weekDay]; 
} 
else if (regular_hours_summer (current)){
    hours.innerHTML = hours_regular[weekDay];
} 
else if (regular_hours_fall (current)){
    hours.innerHTML = hours_regular[weekDay];
} 
else if (short_hours ()) {
    hours.innerHTML = hours_shortened;
} 
else {hours.innerHTML = "<a href='http://www.deltastate.edu/academics/libraries/libraries-hours-of-operation/' target='_blank'>Click here for Library hours</a>"}

0 个答案:

没有答案