This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
58 lines (48 loc) · 1.48 KB
/
script.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
window.addEventListener("load", () => {
clock();
function clock() {
const today = new Date();
// get time components
const hours = today.getHours();
const minutes = today.getMinutes();
const seconds = today.getSeconds();
//add '0' to hour, minute & second when they are less 10
const hour = hours < 10 ? "0" + hours : hours;
const minute = minutes < 10 ? "0" + minutes : minutes;
const second = seconds < 10 ? "0" + seconds : seconds;
//make clock a 12-hour time clock
const hourTime = hour > 12 ? hour - 12 : hour;
// if (hour === 0) {
// hour = 12;
// }
//assigning 'am' or 'pm' to indicate time of the day
const ampm = hour < 12 ? "AM" : "PM";
// get date components
const month = today.getMonth();
const year = today.getFullYear();
const day = today.getDate();
//declaring a list of all months in a year
const monthList = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
//get current date and time
const date = monthList[month] + " " + day + ", " + year;
const time = hourTime + ":" + minute + ":" + second + ampm;
//combine current date and time
const dateTime = date + " - " + time;
//print current date and time to the DOM
document.getElementById("date-time").innerHTML = dateTime;
setTimeout(clock, 1000);
}
});