0

I am trying to show a specific div depending on the day and time.

During the hours of 8am - 7pm Monday to Friday and 8:30am - 4pm on Saturdays, an div with a "we are open" message is to show. Outside of these hours, a closed message div will show.

I have seen other posts ( jquery - show / hide div depending on day and time ) which achieve this for one day but not for two different day groups and different sets of opening hours and I have not been able to work out how to get it to work.

Any help will be greatly appreciated

1
  • 2
    Did you tried anything after looking at the post you've mentioned?
    – Kaushik
    Commented Oct 30, 2019 at 3:55

2 Answers 2

0

You can do something like this:

<script>
function ShowOnTime(element){

    var daysWorking = [{dayWorking: 1, startTimeWorking: '8', endTimeWorking: '19'}, {dayWorking: 2, startTimeWorking: '8', endTimeWorking: '19'}, {dayWorking: 3, startTimeWorking: '8', endTimeWorking: '19'}, {dayWorking: 4, startTimeWorking: '8', endTimeWorking: '19'}, {dayWorking: 5, startTimeWorking: '8', endTimeWorking: '19'}, {dayWorking: 6, startTimeWorking: '830', endTimeWorking: '16'}];
    var a = new Date(); 
    var nowDay = a.getDay(); //0-6 denotes Sunday, monday, tuesday etc etc.
    var nowHours = a.getHours();
    var nowMinutes = a.getMinutes();

    if(daysWorking.find(o => o.dayWorking === nowDay)){
        if (daysWorking.find(o => o.startTimeWorking <= (nowHours+''+nowMinutes)) && daysWorking.find(o => o.endTimeWorking >= (nowHours+''+nowMinutes))) {
            $(element).show();
        } else {
            $(element).hide();
        }
    } else {
        $(element).hide();
    }

}

$(document).ready(function(){
    ShowOnTime("#TestDiv"); 
});
</script>

Hope, this will get you some idea.

0
0

You just need to add one more condition for Saturday in that answer

var Now = new Date('11/4/2019 10:11:00');
 var CurrentDay = Now.getDay();
 if (CurrentDay!=6)
 {
      var OpenHour=8;
      var OpenMin=0;
      var CloseHour=19;
      var CloseMin=0;
 }
 else
 {
 var OpenHour=8;
      var OpenMin=30;
      var CloseHour=16;
      var CloseMin=0;
 }
var  OpeningTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), OpenHour, OpenMin);
var  ClosingTime = new Date(Now.getFullYear(), Now.getMonth(), Now.getDate(), CloseHour, CloseMin);
var  Open = (Now.getTime() > OpeningTime.getTime() && Now.getTime() < ClosingTime.getTime());
if (CurrentDay !== 0 && Open) {
        $('.openstatus').toggle();
    }
.hours {
      display:none;  
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="hours openstatus">We are OPEN</div>
    <div class="closed openstatus">We are CLOSED</div>

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.