0

I been trying to display "Currently opened on Monday - Friday." & going to change to "Currently closed on Saturday - Sunday."

I try to learned by googling but I was not able to achieve:

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);
benvc
  • 14,448
  • 4
  • 33
  • 54
Brandon Powell
  • 99
  • 3
  • 10
  • Please describe your question. – Andrew Li Sep 14 '18 at 00:36
  • 3
    This can be tricky since you probably want to be "open" during certain hours in a particular timezone. Javascript date functions are best when you want to display date and time in the user's local timezone. Best option for timezone aware functions is probably [moment.js](https://momentjs.com/). For an example of what it takes to handle weekday business hours, account for daylight savings, etc in just one timezone, you can take a look at [my recent answer to a related question](https://stackoverflow.com/questions/52303123/display-div-based-on-remote-timezone-and-dates/52307679#52307679). – benvc Sep 14 '18 at 00:38
  • 3
    Also you should take into account the timezone since your code will be running on client browsers which have their own local time. – Kevin He Sep 14 '18 at 00:42
  • 1
    Yeah I have to agree with Kevin. This isn't a good idea in JavaScript. You'll always have to take timezones into account, and that's much easier to do on the backend. – Mike Young Sep 14 '18 at 01:32
  • Holidays, timezones, natural disasters, etc., not good to hard-code in app. It's probably easier to answer this considering when you're *not* open than when you are. – Rafael Sep 14 '18 at 01:37
  • Thank you, you everyone for help advice – Brandon Powell Sep 14 '18 at 08:03

2 Answers2

1

you can use the getDay() method of the Date object to get the day of the week, then you check if it is a day of the week where its opened or not, if its opened then you check the hours.

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

check working example https://jsfiddle.net/93ut5jve/9/ references:

0

Here is a simple solution that could help to point you in the right direction.

I think that one of the problems with your code is that it only captured the hour of the day, and not the day of the week.

Below you can set your open days and hours in the open object, but if you have different open times on different days in the future, you will need to define the open object differently and will have to change how the getStatus function works

// 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);
wfaye
  • 268
  • 1
  • 2
  • 10