forked from plp13/CoolClock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigitalclock.js
More file actions
71 lines (55 loc) · 1.7 KB
/
digitalclock.js
File metadata and controls
71 lines (55 loc) · 1.7 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
59
60
61
62
63
64
65
66
67
68
69
/* Parameters
Time or Date as a single string, or update as separate Hour, Min, Sec etc
*/
const padzero = (num, places) => String(num).padStart(places, '0')
function setValue(field, value) {
fld = document.getElementById(field);
if (fld != null) {
fld.innerHTML = value;
}
}
function showDateTime() {
var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
var dayNames = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ];
var now = new Date()
// Raw output
setValue("dtstringfull", now.toString());
h = padzero(now.getHours(), 2);
m = padzero(now.getMinutes(), 2);
s = padzero(now.getSeconds(), 2);
// ampm = (h < 12) ? "AM" : "PM";
// Time
setValue("hour", h);
setValue("min", m);
setValue("sec", s);
// setValue("ampm", ampm);
setValue("timestring", `${h}:${m}:${s}`);
var y = now.getYear();
if (y < 1000) {y = y + 1900; }
// daynum = padzero(now.getDate(), 2);
daynum = now.getDate();
var suffix = "xx";
if (daynum%10==1) {
suffix = "st"
} else if (daynum%10==2) {
suffix = "nd"
} else if (daynum%10==3) {
suffix = "rd"
} else {
suffix = "th";
}
var y2 = now.getYear()-100;
var m2 = now.getMonth()+1;
var digit_dt = h + ":" + m + ":" + s + " " + daynum + "/" + m2 + "/" + y2;
setValue("dtstring", digit_dt);
// Date
setValue("year", y);
setValue("month", monthNames[now.getMonth()]);
setValue("daynum", daynum);
setValue("suffix", suffix);
setValue("dayname", dayNames[now.getDay()]);
}
function mbDigitalClock() {
showDateTime();
setTimeout("mbDigitalClock()", 1000);
}