-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (71 loc) · 2.39 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function toggler_format_func() {
var text = document.querySelector("button");
// var ap = document.querySelector("am_pm");
var toggle_format = document.querySelector(".toggle");
var val = toggle_format.getAttribute("curr_format");
if (val == "12") {
toggle_format.setAttribute("curr_format", "24");
text.innerHTML = "click to see in 12-hour format";
// ap.innerHTML = "";
}
else {
toggle_format.setAttribute("curr_format", "12");
text.innerHTML = "click to see in 24-hour format";
}
}
function clock() {
var today = new Date();
var hours = today.getHours();
var minutes = today.getMinutes();
var seconds = today.getSeconds();
var toggle_format = document.querySelector(".toggle");
var val = toggle_format.getAttribute("curr_format");
let am_pm = "AM";
// To set am/pm depending on time
if (hours >= 12) {
am_pm = "PM";
}
// to set 12 hour format
// var toggle_format = document.querySelector(".toggle");
var val = toggle_format.getAttribute("curr_format");
if (val == "12") {
if (hours > 12) {
hours = hours % 12;
}
}
// setting extra 0s before single digit
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
// var ap = document.querySelector("am_pm");
document.querySelector(".hour").innerHTML = hours;
document.querySelector(".minutes").innerHTML = minutes;
document.querySelector(".seconds").innerHTML = seconds;
document.querySelector(".hour").innerHTML = hours;
var toggle_format = document.querySelector(".toggle");
// if(ap.innerHTML == "")
var daynum = today.getDate();
var year = today.getFullYear();
var day_name = today.toLocaleString("default", { weekday: "long" });
var mon_name = today.toLocaleString("default", { month: "short" });
document.querySelector(".month").innerHTML = mon_name;
document.querySelector(".day").innerHTML = day_name;
document.querySelector(".day_num").innerHTML = daynum;
document.querySelector(".year").innerHTML = year;
if (val == "24") {
document.querySelector(".am_pm").innerHTML = "";
}
else {
document.querySelector(".am_pm").innerHTML = am_pm;
}
}
var myInterval = setInterval(clock, 1000);
function myStop() {
clearInterval(myInterval);
}