2016-12-29 85 views
0

正在做一个脚本,其中需要知道商店是打开还是关闭,取决于当地的时间。目前,我得到一个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

那么你尝试过这么远吗?我建议你至少在寻求帮助之前尝试一些东西...... – NewToJS

+0

我会先不存储像'Mo-Sa 11:00-14:30'这样友好的数据,然后根据你的想法在白天存储数据。这种友好的数据可以是仅显示的,或者可能在用户输入时进行解析,但如果数据非常简单地存储在数据库中,则对数据的任何查询都会更容易。 –

回答

-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");  

问题/待办事项(我不做所有的工作): 星期日至星期五不工作,for循环将失败。 您需要以某种方式将今天的日期转换为公开参数。

+0

为什么downvote? –

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"], 
 
\t 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){  \t 
 
     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

相关问题