-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.js
39 lines (31 loc) · 1.21 KB
/
clock.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
// Functioning of the Digital Clock
function upnowClock() {
var now = new Date();
var daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var dayOfWeek = daysOfWeek[now.getDay()];
var date = now.getDate();
var month = now.getMonth() + 1; // Months are 0-based
var year = now.getFullYear();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var session = "AM";
if (hours == 0){
hours = 12;
}
if (hours > 12 ){
hours = hours -1 ;
session = "PM";
}
// Add leading zeros if necessary
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
// Upnow the clock element
document.getElementById("clock").textContent = hours + ":" + minutes + ":" + seconds + " "+ session ;
document.getElementById("date").textContent = dayOfWeek + " " + month + "/" + date + "/" + year;
}
// Call the upnowClock function initially
upnowClock();
// Set an interval to upnow the clock every second
setInterval(upnowClock, 1000);