-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.js
63 lines (55 loc) · 1.6 KB
/
clock.js
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
59
60
61
62
63
const clockContainer = document.querySelector("time"),
clockTitle = clockContainer.querySelector("#clock");
FORMAT_LS = "12hour";
const defaultFormat = false;
var format = JSON.parse(localStorage.getItem(FORMAT_LS));
function saveFormat(bool) {
localStorage.setItem(FORMAT_LS,JSON.stringify(bool));
}
function changeFormat() {
if (format === true) {
format = false;
} else {
format = true;
}
saveFormat(format);
}
function configFormat() {
if (format !== null) {
if (format === true) {
hour12();
} else {
hour24();
}
} else {
format = defaultFormat;
saveFormat(format);
}
}
function hour12() {
var date = new Date();
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
const apm = document.createElement("span");
apm.innerText = ampm;
apm.style.fontSize = "2rem";
hours = hours % 12;
hours = hours ? hours : 12;
clockTitle.innerText = `${hours < 10 ? `0${hours}` : hours}:${minutes < 10 ? `0${minutes}` : minutes}`;
clockTitle.appendChild(apm);
saveFormat(format);
}
function hour24() {
var date = new Date();
var minutes = date.getMinutes();
var hours = date.getHours();
clockTitle.innerText = `${hours < 10 ? `0${hours}` : hours}:${minutes < 10 ? `0${minutes}` : minutes}`;
saveFormat(format);
}
function init() {
configFormat();
setInterval(configFormat, 1);
clockTitle.addEventListener("click", changeFormat);
}
init()