-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
46 lines (35 loc) · 1.24 KB
/
app.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
function displayTime() {
var time = new Date();
var hours = time.getHours();
var minutes = time.getMinutes();
var seconds = time.getSeconds();
//for 12hour clock, define a variable meridiem and default to ante meridiem (AM)
var meridiem = " AM";
//since this is a 12 hour clock, once hours increase past 11, i.e., 12 -23, subtract 12 and set the meridiem
//variable to post meridiem (PM)
if (hours>11){
hours = hours - 12;
meridiem = " PM";
}
//at 12PM, the above if statement is set to subtract 12, making the hours read 0.
//create a statement that sets the hours back to 12 whenever it's 0.
if (hours === 0){
hours = 12;
}
//keep hours, seconds, and minutes at two digits all the time by adding a 0.
if (hours<10){
hours = "0" + hours;
}
if (minutes<10){
minutes = "0" + minutes;
}
if (seconds<10){
seconds = "0" + seconds;
}
var clock = document.getElementById('clockDiv');
clock.innerText = hours +":"+ minutes +":"+ seconds + meridiem;
}
//run displayTime function
displayTime();
//set interval to 1000ms (1s), so that info updates every second
setInterval(displayTime, 1000);