-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.js
More file actions
63 lines (51 loc) · 1.58 KB
/
Copy pathclock.js
File metadata and controls
63 lines (51 loc) · 1.58 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
// accessing html elements
const displayTime = document.getElementById("time");
const displayDate = document.getElementById("date");
// getting and displaying the current date
const day = setInterval(showDate, 5000);
function showDate() {
let d = new Date();
function fullMonth(d)
{
var month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
return month[d.getMonth()];
}
let currentDay = d.getDate();
let currentYear = d.getFullYear();
let fullDateString = fullMonth(d) + ' ' + currentDay + ', ' + currentYear;
displayDate.innerHTML = fullDateString;
}
// have to call showDate once or the date won't show at first
showDate();
// getting and displaying the current time
const time = setInterval(showTime, 1000);
function showTime() {
let d = new Date();
let hours = d.getHours();
let minutes = d.getMinutes();
let seconds = d.getSeconds();
let meridian = "am";
let noon = 12;
//changing from AM to PM
if (hours >= noon) {
meridian = "pm";
}
//avoiding displaying military time for this project
if (hours > noon)
{
hours = hours - 12;
}
//avoiding single digits in the time
hours = updateTime(hours);
minutes = updateTime(minutes);
seconds = updateTime(seconds);
fullTimeString = hours + ":" + minutes + ":" + seconds + " " + meridian;
displayTime.innerHTML = fullTimeString;
}
function updateTime(n) {
if (n < 10) {
return "0" + n;
} else {
return n;
}
}