I want to know how to toggle a div on correct time and week days. Example:- when sunday 12:00pm div1 will show, in monday 12:00 div2 will show others time stay hidden. This code works fine hide show on correct time but i also want to add Week days to show hide like if (day== sunday || n > '08:59' && n < '09:59'){}
JavaScript
x
60
60
1
function running() {
2
Object.prototype.twoDigits = function () {
3
return ("0" + this).slice(-2);
4
}
5
6
// get current date and time
7
let now = new Date();
8
n = now.getHours().twoDigits() + ':' + now.getMinutes().twoDigits();
9
10
if (n > '08:59' && n < '09:59') {
11
$(".class-1").addClass("now").removeClass("old");
12
$('.class-1 .overlay').hide();
13
$('.class-2 .overlay').show();
14
$('.class-3 .overlay').show();
15
$('.class-4 .overlay').show();
16
$('.class-1 .overlay-2').hide();
17
$('.class-2 .overlay-2').hide();
18
$('.class-3 .overlay-2').hide();
19
$('.class-4 .overlay-2').hide();
20
}
21
else if (n > '09:59' && n < '10:59') {
22
$(".class-2").addClass("now").removeClass("old");
23
$('.class-1 .overlay').hide();
24
$('.class-2 .overlay').hide();
25
$('.class-3 .overlay').show();
26
$('.class-4 .overlay').show();
27
$('.class-1 .overlay-2').show();
28
$('.class-2 .overlay-2').hide();
29
$('.class-3 .overlay-2').hide();
30
$('.class-4 .overlay-2').hide();
31
32
}
33
else if (n > '10:59' && n < '11:29') {
34
35
$('.class-1 .overlay').hide();
36
$('.class-2 .overlay').hide();
37
$('.class-3 .overlay').show();
38
$('.class-4 .overlay').show();
39
$('.class-1 .overlay-2').show();
40
$('.class-2 .overlay-2').show();
41
$('.class-3 .overlay-2').hide();
42
$('.class-4 .overlay-2').hide();
43
44
}
45
46
else {
47
$(".class-1").addClass("old").removeClass("now");
48
$(".class-2").addClass("old").removeClass("now");
49
$(".class-3").addClass("old").removeClass("now");
50
$(".class-4").addClass("old").removeClass("now");
51
$('.hooray').show();
52
$('.overlay').show();
53
$('.overlay-2').hide();
54
55
}
56
}
57
$(document).ready(function () {
58
running();
59
});
60
Advertisement
Answer
You can use const d = now.getDay()
which returns 0 – 6 where 0 = Sunday. Then you can chech for specific day in your if statements like if(d === 3 && … )
to check wednesday.