0

我想知道在商店的正确营业时间内验证日期的最佳方法是什么...

我正在为一位高尔夫专业人士创建一个网站,并正在接受有关课程的预订。我已将 fullCalendar 用于日历并将其与谷歌日历集成,因此可以轻松地将其同步到专业人士的手机等。

作为日历的一部分,当客户点击一个时间段时,fullcalendar 会为我提供一个日期对象。我想验证这个选定的时段是否在商店的营业时间内,以便上课等。

时间将在某个时候从数据库表中检索,但如果效果最好,我可以将它们以 JSON 格式返回到页面上 - 但这不是这个问题的目的......

营业时间因星期几和“季节”而异 [夏令时 -vs- 冬令时]

ie:
 Summer Times
    Monday = 10:00 - 21:00
    Tuesday = 10:00 - 21:00
    Wednesday = 10:00 - 21:00
    Thursday = 10:00 - 21:00
    Friday = 10:00 - 21:00
    Saturday = 08:00 - 18:00
    Sunday = 08:00 - 18:00

 Winter Times
    Monday = CLOSED
    Tuesday = 10:00 - 18:30
    Wednesday = 10:00 - 18:30
    Thursday = 12.00 – 20.30
    Friday = 12.00 – 20.30
    Saturday = 08:00 - 18:00
    Sunday = 08:00 - 18:00

验证这一点的“最佳”方法是什么?

最好的选择是简单地做嵌套的 if/switch 语句..

[sudo code]
if(summer}
{
    switch(date.getDay())
    {
    case 1:
          if(time > mondayOpeningTime && time < mondayCloseTime)
          {
            return true;
          }
          else
          {
            return false;
          }
      break;
    case 2:
      execute code block 2
      break;
    .....
    default:
      code to be executed if n is different from case 1 and 2
    }
}
else
{
    // Same as summer logic but for winter times etc..
} 

或者有没有更好的方法来做到这一点?

为长篇文章/问题道歉,但提前感谢任何花时间阅读和回复它的人:)

克里斯

4

2 回答 2

2

这是我写的东西。相当粗糙,但考虑到关闭时间在午夜之后的情况。

也可在 Gist 获得:https ://gist.github.com/vtntimo/b5c724371bfb27bfb080

如果你创造了改进,请联系我:)

/*
    Operating hours check
    =====================================

    - Checks if something is open right now
    - Supports closing times going over midnight
    - Feel free to use in whatever you need :)
    - If you make improvements, please inform me!

    Example:

    // Setting operating hours
    // Key 0 is sunday, 1 is monday and so on
    var hours = [
        // Sunday
        {
            open: '08:00',
            close: '16:00'
        },

        // Monday
        {
            allday: true // Open all day
        },

        // Tuesday
        false, // Closed all day

        // Wednesday
        {
            open: '08:00',
            close: '16:00'
        },

        // Thursday
        {
            open: '08:00',
            close: '16:00'
        },

        // Friday
        {
            open: '08:00',
            close: '16:00'
        },

        // Saturday
        {
            open: '08:00',
            close: '16:00'
        },
    ];

    if(isOpen(hours)) {
        alert("We're open!");
    }
    else {
        alert("We're closed!");
    }
*/
function isOpen(hours) {
    var now = new Date();
    var today = now.getDay();

    var yesterday = today - 1;
    if(yesterday < 0) yesterday = 6;

    // Check today's opening hours
    if(hours[today]) {

        // It's open all day today
        if(hours[today].allday) {
            return true;
        }

        // Not open all day
        else {
            var open = hours[today].open;
            var close = hours[today].close;

            // Check if the location is open
            if(checkOpenHours(now,now,open,close)) {
                return true;
            }
        }

    }

    // Check yesterday's opening hours in case of closing after midnight
    if(hours[yesterday]) {

        // Not open all day (possibly closing after midnight)
        if(!hours[yesterday].allday) {
            var open = hours[today].open;
            var close = hours[today].close;

            var yesterday_date = new Date(now.getFullYear(), now.getMonth(), (now.getDate()-1), 0, 0, 0, 0);
            if(checkOpenHours(now,yesterday_date,open,close)) {
                return true;
            }
        }

    }

    // Not open
    return false;
}

/*
    Check if "now" is within operating hours
*/
function checkOpenHours(now, operatingDate, open, close) {
    // Splitting times to array
    var o = open.split(":");
    var c = close.split(":");

    // Hours not in proper format
    if(o.length < 2 || c.length < 2) {
        return false;
    }

    // Converting array values to int
    for(var i = 0; i < o.length; i++) {
        o[i] = parseInt(o[i]);
    }
    for(var i = 0; i < c.length; i++) {
        c[i] = parseInt(c[i]);
    }

    // Set opening Date()
    var od = new Date(operatingDate.getFullYear(), operatingDate.getMonth(), operatingDate.getDate(), o[0], o[1], 0, 0);

    // Set closing Date()
    var closingDay = operatingDate.getDate();

    // Closing after midnight, shift day to tomorrow
    if(o[0] > c[0]) {
        closingDay++;
    }
    var cd = new Date(operatingDate.getFullYear(), operatingDate.getMonth(), closingDay, c[0], c[1], 0, 0);

    // Is within operating hours?
    if(now > od && now < cd) {
        return true;
    }
    else return false;
}
于 2015-02-15T09:03:29.057 回答
0

您需要 2 个数组:openingTimes 和 closingTimes。第一个维度是季节,第二个维度是星期几。那么它只是:

return ((time > openingTimes[season, dayOfWeek]) && (time < closingTimes[season, dayOfWeek]));
于 2013-01-22T21:53:22.487 回答