-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathscript.js
68 lines (54 loc) · 1.66 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
58
59
60
61
62
63
64
65
66
67
68
// Function to start the clock when the page loads
window.onload = function(){
startClock();
}
// Get the element with id="time"
let time = document.getElementById('time');
// getting the today's date
let currentData = new Date();
// assigning the date, day, month and year to the variables
let date = currentData.getDate();
let day = currentData.getDay();
let month = currentData.getMonth();
let year = currentData.getFullYear();
// Array of days to display the day of the week according to the number
let days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
day = days[day];
// Main function to start the clock
function startClock(){
let currentData = new Date();
let seconds = currentData.getSeconds();
let minutes = currentData.getMinutes();
let hours = currentData.getHours();
// Conditions to check AM or PM
let AMorPM;
if(hours>=12){
AMorPM = "PM";
}
else{
AMorPM = "AM";
}
let h = hours;
if(hours==0){
h = 12;
}
// if the hours are greater than 12 then subtract 12 to get the 12 hour format
else if(hours>12){
h = hours-12;
}
let s = check(seconds);
let m = check(minutes);
function check(val){
if(val<10){
return '0' + val;
}
return val;
}
// Displaying the time in the element with id="time"
time.innerText = h + ':' + m + ':' + s + ' '+AMorPM;
}
// Writing the date in the "date" element
document.getElementById('date').innerText = date+'/ '+month+'/ '+year;
document.getElementById('day').innerText = day;
// Calling the function every second
setInterval(startClock,1000);