-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrentTime.html
36 lines (33 loc) · 1.78 KB
/
currentTime.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="icon" type="image/x-icon" href="https://file.garden/ZXPZOz_dI1vYUjXR/IMG_2148.ico" />
<title>Current Time</title>
</head>
<body>
<h1 id="currentTime"></h1>
<script>
"use strict"; // Makes the JS be executed in strict mode.
// Declaring the variables to make them global
let d;
let h;
let m;
let s;
let ampm;
setInterval(function () { // sets an interval so that this code block executes every second.
d = new Date(); // sets d as the current date.
h = d.getHours(); // sets h as the current hour in a number from 1 to 23
if (h > 12) { // executes this code block if the current hour is more than 12.
ampm = "PM"; // sets ampm to "PM".
d.setHours(h - 12); // sets the hour to the current hour minus 12 to make it a 12 format.
h = d.getHours(); // updates the hours to get the hours in a 12-hour format.
} else { // executes this code block if the current hour is not more than 12.
ampm = "AM"; // Sets ampm to "AM".
}
m = d.getMinutes(); // sets m to the current minutes.
s = d.getSeconds(); // sets s to the current seconds.
document.getElementById("currentTime").innerHTML = `${h}:${m}:${s} ${ampm}`; // sets the innerHTML of the element with the id "currentTime" to the current time.
}, 1000) // specifies that this code block should be executed every second aka every 1000 milliseconds.
</script>
</body>
</html>