I have a script that shows different div depending on the day of the week and the time of day. He works well. I need to modify it. I want to set different opening hours each day. How can I do this? Please help.
Example
Monday open from 8:00 to 16:00
Tuesday open from 8:00 to 16:00
Wednesday open from 8:00 to 18:00
Thursday open from 8:00 to 18:00
Friday open from 8:00 to 16:00
Saturday close
Sunday open from 8:00 to 13:00
My script:
var d = new Date();
var dayOfWeek = d.getDay();
var hour = d.getHours();
var mins = d.getMinutes();
var status = 'open';
if (dayOfWeek !== 6 && dayOfWeek !== 0 && hour >= 9 && hour <= 15){
if (hour=='9' && mins < '00'){
status = 'closed';
}else if (hour=='15' && mins > '30'){
status = 'closed';
}else{
status = 'open';
}
}else{
status = 'closed';
}
if (status=='open') {
$('.hours').show();
$('.closed').hide();
}else{
$('.hours').hide();
$('.closed').show();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hours">We are OPEN</div>
<div class="closed">We are CLOSED</div>
.show()
and.hide()
, you could use.toggle(status === 'open' / status !== 'open')
. Then you can remove theif
;-) – Cerbrus Sep 1 '17 at 14:55