使用JavaScript建立工作时间

时间:2018-09-14 00:30:22

标签: 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.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);

2 个答案:

答案 0 :(得分:1)

您可以使用getDay()对象的Date方法来获取星期几,然后检查是否在星期几中打开了它,是否打开了它。您检查小时数。

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')
  }
}

查看工作示例https://jsfiddle.net/93ut5jve/9/ 参考:

答案 1 :(得分:0)

这是一个简单的解决方案,可以帮助您指出正确的方向。

我认为您的代码的问题之一是它仅捕获一天中的小时,而不捕获星期几。

下面,您可以在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);