This repository was archived by the owner on Feb 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
36 lines (29 loc) · 1.28 KB
/
Copy pathscript.js
File metadata and controls
36 lines (29 loc) · 1.28 KB
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
document.addEventListener('DOMContentLoaded', () => {
const clockElement = document.getElementById('clock');
let is12HourFormat = true;
function clock() {
const clockTime = document.getElementById('date-time');
const today = new Date();
// get time components
const hours = today.getHours();
const minutes = today.getMinutes();
const date = today.getDate();
const month = today.toLocaleString('default', { month: 'short' });
const year = today.getFullYear().toString().substring(2);
//add '0' to hour & minute when they are less 10
const hour = hours < 10 ? '0' + hours : hours;
const minute = minutes < 10 ? '0' + minutes : minutes;
//make clock a 12-hour time clock
let hourTime = is12HourFormat ? (hours % 12 || 12) : hours;
//assigning 'am' or 'pm' to indicate time of the day
const ampm = hour < 12 ? ' AM' : ' PM';
const time = hourTime + ':' + minute + (is12HourFormat ? ampm : '');
const fullDate = date + '-' + month + '-' + year
clockTime.innerHTML = `${time}<br>${fullDate}`;
setTimeout(clock, 1000);
}
clockElement.addEventListener('click', () => {
is12HourFormat = !is12HourFormat;
});
clock();
});