Repository navigation
Expand file tree
/
Copy pathapltv-clock.js
More file actions
58 lines (48 loc) · 1.96 KB
/
Copy pathapltv-clock.js
File metadata and controls
58 lines (48 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Create clock div
var clockDiv = document.createElement('div');
clockDiv.setAttribute('id', 'digitalClock');
clockDiv.style.position = 'fixed';
clockDiv.style.bottom = '0px';
clockDiv.style.right = '0px';
clockDiv.style.padding = '10px';
clockDiv.style.background = 'rgba(0, 0, 0, 0.5)';
clockDiv.style.color = '#fff';
clockDiv.style.fontFamily = 'Arial, sans-serif';
clockDiv.style.zIndex = '9999';
document.body.appendChild(clockDiv);
var libraryHours = {
0: {close: 17}, // Sunday
1: {close: 20}, // Monday
2: {close: 20}, // Tuesday
3: {close: 20}, // Wednesday
4: {close: 20}, // Thursday
5: {close: 17}, // Friday
6: {close: 17}, // Saturday
};
function updateClock() {
var now = new Date();
var day = now.getDay();
var hours = now.getHours();
var minutes = now.getMinutes();
// Check if the library closes within 30 minutes
var closingHour = libraryHours[day].close;
var closingDisplayHour = closingHour % 12 || 12; // This will convert closingHour into 12 hour format.
var closingAmPm = closingHour >= 12 ? 'pm' : 'am';
var currentTimeInMinutes = hours * 60 + minutes;
var closingTimeInMinutes = closingHour * 60;
// Convert from 24h to 12h format
var displayHours = hours % 12;
displayHours = displayHours ? displayHours : 12; // the hour '0' should be '12'
// Add a zero in front of numbers<10
var displayMinutes = minutes < 10 ? '0' + minutes : minutes;
var amPm = hours >= 12 ? 'pm' : 'am';
var timeStr = displayHours + ':' + displayMinutes + amPm;
// Check if the library is closing within the next 30 minutes
if (currentTimeInMinutes >= (closingTimeInMinutes - 30) && currentTimeInMinutes < closingTimeInMinutes){
var closingStr = 'The Library closes at ' + closingDisplayHour + ':00' + closingAmPm + '. ';
timeStr = closingStr + 'The current time is: ' + timeStr;
}
clockDiv.innerText = timeStr;
}
setInterval(updateClock, 1000);
;