|
1 | | -function startTime(){ |
2 | | - //Function to start displaying current time |
3 | | - |
4 | | - //Get the current date and time |
5 | | - const date = new Date(); |
6 | | - const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; |
7 | | - const dayArr = ["Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; |
8 | | - |
9 | | - |
10 | | - //Extract hours, minutes, seconds from the current time |
11 | | - let h = date.getHours(); |
12 | | - let m = date.getMinutes(); |
13 | | - let s = date.getSeconds(); |
14 | | - let am_pm = "AM"; |
15 | | - |
16 | | - |
17 | | - //Setting time for 12 hour format |
18 | | - |
19 | | - // if(h > 12){ |
20 | | - // h = h - 12; |
21 | | - // am_pm = "PM"; |
22 | | - // }else if (h==12){ |
23 | | - // h = 12; |
24 | | - // am_pm = "AM"; |
25 | | - // } |
26 | | - |
27 | | - if (h >= 12) { |
28 | | - if (h > 12) h -= 12; |
29 | | - am_pm = "PM"; |
30 | | - } else if (h == 0) { |
31 | | - h = 12; |
32 | | - am_pm = "AM"; |
33 | | - } |
34 | | - |
35 | | - //Call the checkTime() function to add leading zeroes when needed |
36 | | - m = checkTime(m); |
37 | | - s = checkTime(s); |
38 | | - h = checkTime(h); |
39 | | - |
40 | | - //This setTimeOut() function is used to call the startTime() function every 1 second (1000 miliseconds) |
41 | | - setTimeout(startTime, 1000); |
42 | | - |
43 | | - //Displaying the time using Document Object Model in the browser window |
44 | | - document.getElementById("txt").innerHTML = h + ":" + m + ":" + s + " " + am_pm; |
45 | | - document.getElementById("day").innerHTML = `${dayArr[date.getDay()]} <br> ${date.getDate()} ${monthNames[date.getMonth()]} ${date.getFullYear()}`; |
| 1 | +const separator = ":"; |
| 2 | +const clockContainer = document.getElementById("txt"); |
| 3 | +const dateContainer = document.getElementById("day"); |
| 4 | +const monthNames = [ |
| 5 | + "January", |
| 6 | + "February", |
| 7 | + "March", |
| 8 | + "April", |
| 9 | + "May", |
| 10 | + "June", |
| 11 | + "July", |
| 12 | + "August", |
| 13 | + "September", |
| 14 | + "October", |
| 15 | + "November", |
| 16 | + "December", |
| 17 | +]; |
| 18 | +const dayArr = [ |
| 19 | + "Sunday", |
| 20 | + "Monday", |
| 21 | + "Tuesday", |
| 22 | + "Wednesday", |
| 23 | + "Thursday", |
| 24 | + "Friday", |
| 25 | + "Saturday", |
| 26 | +]; |
46 | 27 |
|
| 28 | +function startTime() { |
| 29 | + //Function to start displaying current time |
| 30 | + |
| 31 | + //Get the current date and time |
| 32 | + const date = new Date(); |
| 33 | + |
| 34 | + //Extract hours, minutes, seconds from the current time |
| 35 | + let h = date.getHours(); |
| 36 | + let m = date.getMinutes(); |
| 37 | + let s = date.getSeconds(); |
| 38 | + let am_pm = "AM"; |
| 39 | + |
| 40 | + //Setting time for 12 hour format |
| 41 | + |
| 42 | + // if(h > 12){ |
| 43 | + // h = h - 12; |
| 44 | + // am_pm = "PM"; |
| 45 | + // }else if (h==12){ |
| 46 | + // h = 12; |
| 47 | + // am_pm = "AM"; |
| 48 | + // } |
| 49 | + |
| 50 | + if (h >= 12) { |
| 51 | + if (h > 12) h -= 12; |
| 52 | + am_pm = "PM"; |
| 53 | + } else if (h == 0) { |
| 54 | + h = 12; |
| 55 | + am_pm = "AM"; |
| 56 | + } |
| 57 | + |
| 58 | + //Call the checkTime() function to add leading zeroes when needed |
| 59 | + m = checkTime(m); |
| 60 | + s = checkTime(s); |
| 61 | + h = checkTime(h); |
| 62 | + |
| 63 | + //This setTimeOut() function is used to call the startTime() function every 1 second (1000 miliseconds) |
| 64 | + setTimeout(startTime, 1000); |
| 65 | + |
| 66 | + //Displaying the time using Document Object Model in the browser window |
| 67 | + const clockElement = `${h}${separator}${m}${separator}${s} ${am_pm}`; |
| 68 | + const dateElement = `${dayArr[date.getDay()]} <br> ${date.getDate()} ${ |
| 69 | + monthNames[date.getMonth()] |
| 70 | + } ${date.getFullYear()}`; |
| 71 | + |
| 72 | + clockContainer.innerHTML = clockElement; |
| 73 | + dateContainer.innerHTML = dateElement; |
47 | 74 | } |
48 | 75 |
|
49 | 76 | //Function to add leading zeroes if the number is less then 10 |
50 | | -function checkTime(i){ |
51 | | - if(i < 10){ |
52 | | - i = "0" + i; |
53 | | - } |
54 | | - return i; |
| 77 | +function checkTime(i) { |
| 78 | + if (i < 10) { |
| 79 | + i = "0" + i; |
| 80 | + } |
| 81 | + return i; |
55 | 82 | } |
0 commit comments