使用javascript构建营业时间

使用javascript构建营业时间,javascript,html,datetime,Javascript,Html,Datetime,我一直试图显示“当前周一至周五开放”&将更改为“当前周六至周日关闭” 我试图通过谷歌搜索学习,但未能实现: window.onload = function status() { var date = new Date(); console.log(date); //var day = date.getDay(); var hour = date.getHours();// 0 = 12am, 1 = 1am, ... 18 = 6pm\ console.l

我一直试图显示“当前周一至周五开放”&将更改为“当前周六至周日关闭”

我试图通过谷歌搜索学习,但未能实现:

window.onload = function status() {
    var date = new Date();
    console.log(date);
  //var day  = date.getDay();
    var hour = date.getHours();// 0 = 12am, 1 = 1am, ... 18 = 6pm\
    console.log(hour);

   // check if it's between 9am and 11pm
   if(hour > 12 ) {
      document.getElementById('example').innerHTML = "Currently opened on Monday - Friday.";
    } else if (hour < 23 ) {
      document.getElementById('example').innerHTML = "Currently closed on Saturday - Sunday.";
    } else {
      console.log('Today is not a weekend and hour is between 12 - 23')
    }
  };

setInterval(status, 1000);
console.log(status);
window.onload=函数状态(){
变量日期=新日期();
控制台日志(日期);
//var day=date.getDay();
var hour=date.getHours();//0=12am,1=1am,…18=6pm\
控制台日志(小时);
//检查是否在上午9点到晚上11点之间
如果(小时>12){
document.getElementById('example').innerHTML=“当前在周一至周五打开。”;
}否则,如果(小时<23){
document.getElementById('example').innerHTML=“当前在周六-周日关闭。”;
}否则{
log('今天不是周末,时间在12-23'之间)
}
};
设置间隔(状态,1000);
控制台日志(状态);

这里有一个简单的解决方案,可以帮助您找到正确的方向

我认为您的代码的问题之一是它只捕获一天中的小时,而不是一周中的某一天

您可以在下面的
open
对象中设置您的开放日和开放时间,但是如果您将来在不同的日子有不同的开放时间,则需要以不同的方式定义
open
对象,并且必须更改
getStatus
功能的工作方式

// set up the interval so that the time can be started and stopped as needed
    var interval;

// set the days and times when open (this could be set up differently, for example it could be a range instead)
    var open = {
        days: [1, 2, 3, 4, 5],
        hours: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
    }

// given a date, return a message determining if open
    var getStatus = function(currentDate){
        var hour = currentDate.getHours();
        var day = currentDate.getDay();
        var nowIsOpenDay = open.days.indexOf(day) > -1;
        var nowIsOpenHour = open.hours.indexOf(hour) > -1;

        var message = (nowIsOpenDay && nowIsOpenHour) ? 'Currently opened' : 'Currently closed';

        return {
            'message': message,
            'dateInfo': {
                'hour': hour,
                'day': day,
                'nowIsOpenDay': nowIsOpenDay,
                'nowIsOpenHour': nowIsOpenHour
            }
        }

    }

// run the timer and get the current status
    var startInterval = function(updateInterval){
        updateInterval = (typeof updateInterval === 'undefined') ? 1000 : updateInterval;
        interval = setInterval(function(){
            var currentStatus = getStatus(new Date());
            console.log(currentStatus.message)
            console.log(currentStatus.dateInfo.hour, currentStatus.dateInfo.day)
        }, updateInterval);
    }

// optionall stop the interval
    var stopInterval = function(){
        clearInterval(interval);
    }

// start
    startInterval(2000);

您可以使用
Date
对象的
getDay()
方法来获取一周中的某一天,然后检查该天是否为一周中打开的某一天,如果该天已打开,则检查小时数

function status() {
  var date = new Date();
  var day = date.getDay();
  var hour = date.getHours();
  //check if its sunday or saturday
  if (day == 0 || day == 6) {
    document.getElementById('example').innerHTML = "Currently closed on Saturday - Sunday.";
  // check if its between 9am and 11pm (inclusive)
  } else if (hour >= 9 && hour <= 23) {
    document.getElementById('example').innerHTML = "Currently opened on Monday - Friday.";
  } else {
    console.log('Today is not a weekend and hour is between 12 - 23')
  }
}
功能状态(){
变量日期=新日期();
var day=date.getDay();
var hour=date.getHours();
//看看是星期天还是星期六
如果(日=0 | |日=6){
document.getElementById('example').innerHTML=“当前在周六-周日关闭。”;
//检查其是否在上午9点至晚上11点(含)之间

}否则,如果(小时>=9&&hour,请描述您的问题。这可能很棘手,因为您可能希望“开放”在特定时区的特定时间内。当您希望在用户的本地时区中显示日期和时间时,Javascript日期函数是最好的。时区感知函数的最佳选项可能是。例如,在一个时区中处理工作日营业时间、计算夏令时等需要什么,您可以查看另外,你应该考虑时区,因为你的代码将在有自己本地时间的客户端浏览器上运行。是的,我同意Kevin的观点。这在JavaScript中不是一个好主意。你必须始终考虑时区,这在后端更容易做到。假期、时区、自然灾害等,而不是在应用程序中编写硬代码很好。考虑到你没有打开时可能比打开时更容易回答这个问题。谢谢,大家的帮助建议谢谢,大家的帮助建议很高兴它帮助了你。我推荐w3school,它有很多关于web开发的教程,而且它们的教程都是完整、清晰且文档完整的。不要这样做使用w3schools,它们是一个可怕的资源。Mozilla要好得多。