我想知道验证日期的最佳方法是在商店正确的营业时间内...
我正在为高尔夫专业人士创建一个网站,并正在预订课程。我使用了fullCalendar作为日历,并将其与谷歌日历集成在一起,因此可以轻松地与专业人士的手机轻松同步等。
作为日历的一部分,当客户点击时段时,我通过fullcalendar提供日期对象。我想验证这个选定的插槽是否在商店的开放时间内,以便进行课程等。
在某些时候将从db表中检索时间,但如果效果最好,我可以将它们以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..
}
或者有更好的方法吗?
为长篇文章/问题道歉,但提前感谢任何花时间阅读并回复它的人:)
克里斯
答案 0 :(得分: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;
}
答案 1 :(得分:0)
你想要2个数组:openingTimes和closingTimes。第一维将是季节,第二维将是星期几。那就是:
return ((time > openingTimes[season, dayOfWeek]) && (time < closingTimes[season, dayOfWeek]));