我正在做一个脚本,需要知道商店是打开还是关闭,具体取决于本地时间。目前,我获取了api的数据,我保存在我的数据库中并通过Ajax请求调用它。返回的数据(等于我获得的数据):
["Mo-Sa 11:00-14:30", "Mo-Th 17:00-21:30", "Fr-Sa 17:00-22:00"]
我一直在评估转换它的可能性(我还是要看看怎么做):
{
"monday": ["11:00-14:30", "17:00-21:30"],
"tuesday": ["11:00-14:30", "17:00-21:30"],
"wednesday": ["11:00-14:30", "17:00-21:30"],
"thursday": ["11:00-14:30", "17:00-21:30"],
"friday": ["11:00-14:30", "17:00-22:00"],
"saturday": ["11:00-14:30", "17:00-22:00"],
"sunday": null
}
我在这些问题中看到了一些例子:
Create a function to check if a business is open and write text to html
Determine If Business Is Open/Closed Based On Business Hours(php)
在使用任何例程编写代码之前,我想知道是否有人知道如何简化或在网络上看到某些代码;不要重新发明轮子。非常感谢。
祝你好运
答案 0 :(得分:0)
对于开放时间,我更喜欢将要比较的所有值转换为完整分钟(hour*60 + minutes
)。这样就可以更容易地与实际时间进行比较。
以分钟为起点,我会通过在每个工作日使用一个数组(使用Date.getDay()
返回的相同索引)来进行有些不同的转换,每天都包含具有开启的子数组 - 结束时间以分钟为单位(开始和结束也在子数组或对象中)
const arr= ["Mo-Sa 11:00-14:30", "Mo-Th 17:00-21:30", "Fr-Sa 17:00-22:00"],
days = ['Su','Mo','Tu','We', 'Th', 'Fr', 'Sa'], //start with sunday to be compatible with Date.getDay
times = Array.from(days, (d,i) => []),
getDay = (s,i) => days.indexOf(s.slice(i,i+2)), //helper function for parsing day name
getMinutes = s => s.split(':').reduce((m, n) => m * 60 + parseInt(n,10),0); //helper to store time in minutes of day
//convert to new format
for(let s of arr){
let d = getDay(s,0), end = getDay(s,3);
while(true){
times[d].push( s.slice(6).split('-').map(getMinutes));
if(d===end)break;
d = ++d % 7; //the %7 makes it possible to have ranges as Th-Mo
}
}
//now times contains an array with a day in each index, containing subarrays of the opening times in minutes
function isOpen(dt){
let mins = dt.getHours() * 60 + dt.getMinutes();
return times[dt.getDay()].some(a=>a[0] <= mins && a[1] >= mins)
}
//----------------------------------------------------------
//test functions only
console.log('Is open now: ' , isOpen(new Date()));
function test(dts){let dt = new Date(dts); console.log(days[dt.getDay()], dts,':', isOpen(dt));}
test('2016/12/29 8:00'); //th
test('2016/12/29 10:59');
test('2016/12/29 11:00');
test('2016/12/29 12:00');
test('2016/12/30 12:00'); //fr
test('2017/1/1 12:00'); //su
test('2016/12/29 21:45'); //th
test('2016/12/30 21:45'); //fr
答案 1 :(得分:-1)
var dates=yourdateobj;
//its easier to work with numbers then string for example (1-3 is easier then mo-wed)
var daytoindex={"Mo":1,"Tu":2,"Wed":3,"Thu":4,"Fr":5,"Sat":6,"Sun":7};
//the better structured table:
var destructdates=[];
//for each old timestring:
dates.forEach((e,i)=>{
//destructure timestring
e=e.split(" ");
var days=e[0].split("-");
var hours=e[1];
//add time to all days inbetween (1-3 (Mo-Wed) is 1,2,3 (Mo,Tue;Wed)
for(var i=daytoindex[days[0]];i<=daytoindex[days[1]];i++){
//the day is an array,add the open hours
destructdates[i]=destructdates[i]||[];
destructdates[i].push(hours);
}
});
这会创建你的第二个对象(类似):
destructdates:
[
1:["12:33-15:44","12:33-0:30"] //Mo
2:...
]
现在你可以这样做:
function open(day,hour,second){
//get the todays times Array
var dayhours=destructdates[daytoindex[day]];
//if now falls into one of the times:
return dayhours.some((e,i)=>{
//destructure the times:
e=e.split("-");
var start=e[0].split(":");
var starthour= +start[0];
var startminute= +start[1];
var end=e[1].split(":");
var endhour= +end[0];
var endminute= +end[1];
//check:
if(starthour<=hour && startminute<=minute && endhour>=hour &&endminute>=minute){
return true;
}
return false;
});
}
像这样使用:
alert(open("Tu",12,33)?"Open":"Close");
问题/ TODO(我不做你所有的工作): 周日 - 周五不会工作,for循环将失败。 你需要以某种方式将今天的日期转换为开放的参数。