-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.js
More file actions
33 lines (22 loc) · 1.07 KB
/
time.js
File metadata and controls
33 lines (22 loc) · 1.07 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
const currentDate = new Date();
const year = currentDate.getFullYear();
const month = String(currentDate.getMonth() + 1).padStart(2, '0'); // Months are 0-based
const day = String(currentDate.getDate()).padStart(2, '0');
const formattedDate = `${year}-${month}-${day}`;
document.getElementById('current-date').textContent = formattedDate;
function updateTime() {
// Get the current time
const currentTime = new Date();
// Extract time components
const hours = String(currentTime.getHours()).padStart(2, '0');
const minutes = String(currentTime.getMinutes()).padStart(2, '0');
const seconds = String(currentTime.getSeconds()).padStart(2, '0');
// Format the time as HH:MM:SS
const formattedTime = `${hours}:${minutes}:${seconds}`;
// Display the time in the HTML element with id "current-time"
document.getElementById('current-time').textContent = formattedTime;
}
// Update the time every second
setInterval(updateTime, 1000);
// Initial call to display the time immediately
updateTime();