如何使此JavaScript在午夜营业时间内工作



我使用以下javascript根据营业时间查看业务是否开放。 它很简单而且效果很好,但是我如何在午夜过后的时间(例如凌晨 0:30 或 1:00)完成这项工作。不幸的是,它仅适用于 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:原始脚本来自堆栈溢出Javascript商店开放时间的另一篇帖子

更新版本 我更改了脚本,因为我使用了来自 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 这样的事情,这怎么行不通。

经过测试,我自己找到了一种方法。我最终创建了一个小函数。您可以将开放和关闭时间发送到功能,并在业务开放或关闭时获得回复。适用于所有时间,也适用于午夜。

无论如何,此脚本不适用于其他时区的业务,因为它不考虑任何基于位置的时间。如果您用于客户端与业务位于同一时区的本地业务,则可以正常工作。

   // 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/中使用不同的时间进行测试

您可以使用本地时间或使用日期库,如 http://www.datejs.com/或 https://momentjs.com/

根据此日期时间解析(new Date).toLocaleString()和处理数据。

var date = new Date();
var formatted = date.toLocaleString('de-DE');

要获取时区,您可以使用navigator.language

你可以尝试这样的事情。

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

最新更新