我使用以下javascript来查看业务是否根据营业时间打开。它很简单,效果很好,但是如何在凌晨0:30或凌晨1点的午夜时分完成这项工作。不幸的是,它仅适用于17:00-23:00但不适用于17:00-030:
var d = new Date();
var n = d.getDay();
var now = d.getHours() + "." + d.getMinutes();
var weekdays = [
["Sunday", 9.30, 12.00, 15.30,22.00],
["Monday", 8.30, 12.00, 15.30,19.00],
["Tuesday", 8.30, 12.00, 15.30,19.00],
["Wednesday", 8.30, 12.00, 15.30,19.00],
["Thursday", 8.30, 12.00, 15.30,19.00],
["Friday", 8.30, 11.30],
["Saturday"] // we are closed, sorry!
];
var day = weekdays[n];
if (now > day[1] && now < day[2] || now > day[3] && now < day[4]) {
console.log("We're open right now!");
}
else {
console.log("Sorry, we're closed!");
}
我知道这个脚本不支持时区,但这对我来说并不重要,因为这只适用于与客户永远处于同一时区的本地商家。
提前谢谢。 PS:原始脚本来自stackoverflow Javascript Store Opening Hours 中的另一篇帖子更新版本我更改了脚本,因为我使用了来自firebase的json数据并且已经获得了当天的时间,所以我不需要带有日期的数组。
<script>
var d = new Date();
var n = d.getDay();
var now = d.getHours() + "." + d.getMinutes();
// this is what I get from firebase already
// so I dont need to loop throught the days but throught the 2 different opening times.
var day = {
"from": 8.30,
"to": 10,
"from2": 14.30,
"to2": 23
};
console.log('Now: ' + now);
if (now > day.from && now < day.to || now > day.from2 && now < day.to2) {
console.log("We're open right now!");
} else {
console.log("Sorry, we're closed!");
}
</script>
如果我有17:00-01:00之类的东西,这怎么可能不起作用。
答案 0 :(得分:0)
您可以使用本地时间或使用日期库,例如http://www.datejs.com/或https://momentjs.com/
解析(new Date).toLocaleString()
并根据此日期时间处理您的数据。
var date = new Date();
var formatted = date.toLocaleString('de-DE');
要获取时区,您可以使用navigator.language
答案 1 :(得分:0)
你可以尝试这样的事情。
var d = new Date();
var n = d.getDay();
var now = d.getHours() + "." + d.getMinutes();
var weekdays = [
["Sunday", 9.30, 12.00, 15.30,2.00],
["Monday", 8.00, 12.00, 15.30,19.00],
["Tuesday", 8.30, 12.00, 15.30,19.00],
["Wednesday", 8.30, 12.00, 15.30,19.00],
["Thursday", 8.30, 12.00, 15.30,19.00],
["Friday", 8.30, 11.30],
["Saturday"] // we are closed, sorry!
];
var day = weekdays[n];
if (now < day[1] && now > day[2] || now < day[3] && now > day[4]) {
console.log("Sorry, we're closed!");
}
else {
if(n != 0){
day = weekdays[n-1];
}
else{
day = 6;
}
if ((now > day[4])) {
console.log("Sorry, we're closed!");
}
else {
console.log("We're open right now!");
}
}
答案 2 :(得分:0)
经过测试后,我自己找到了一条路。我最终创建了一个小功能。您可以将打开和关闭时间发送给该功能,并在业务开放或关闭时获得回复。适用于所有时间,也适用于午夜。
但是,此脚本不适用于其他时区的业务,因为它不会考虑任何基于位置的时间。如果您使用本地业务,客户在同一时区,因此业务正常。
// Example with JSON data for the current day.
var day = {
"from": 17.30,
"to": 1.30
};
var check_now = check_business_status(day.from, day.to);
console.log('The bussines is ' + check_now);
function check_business_status(from, to) {
var d = new Date();
var n = d.getDay();
var now = d.getHours() + "." + d.getMinutes();
console.log('Now: ' + now);
console.log(from + ' // ' + to);
// First check if to date is smaller than from date and their for tomorrow!
if (from > to){
console.info('Closing time is after midnight! Special Check...');
var status = '';
if (now < to && from > now) {
status = "open";
} else if (now > to && from > now) {
status = "closed";
} else if (now > to && from < now && now > from) {
status = "open";
}
} else {
console.info('Closing time is before midnight! Normal Check...');
if (now > from && now < to) {
status = "open";
} else {
status = "closed";
}
}
return status;
}
您还可以在jsfiddle https://jsfiddle.net/oliver2000/4pdogett/1/
中的不同时间进行测试