-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix] #170 I had fix the bug of 24-hour-format which was previously showing 12-hour-format. And also some styling improvement has been done. ![Screenshot (1168)](https://github.com/Git21221/JS-beginner-projects/assets/110921916/1577d7c5-a8eb-4792-a722-7ad75f272300)
- Loading branch information
Showing
3 changed files
with
138 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,32 @@ | ||
// const clock = document.querySelector(".clock"); | ||
// setInterval(() => { | ||
// let date = new Date(); | ||
// clock.innerHTML = date.toLocaleTimeString(); | ||
// }, 1000); | ||
|
||
|
||
const clock = document.querySelector(".clock"); | ||
const clock12 = document.querySelector(".twelve"); | ||
const clock24 = document.querySelector(".twentyfour"); | ||
const day = document.querySelector(".day"); | ||
|
||
|
||
function updateClock() { | ||
let date = new Date(); | ||
|
||
// 24-hour format | ||
const time24Hour = date.toLocaleTimeString(); | ||
|
||
|
||
const hours = String(date.getHours()).padStart(2, '0'); | ||
const minutes = String(date.getMinutes()).padStart(2, '0'); | ||
const seconds = String(date.getSeconds()).padStart(2, '0'); | ||
|
||
const time24Hour = `${hours}:${minutes}:${seconds}`; | ||
|
||
// 12-hour format | ||
|
||
const options12Hour = { hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true }; | ||
const time12Hour = date.toLocaleTimeString(undefined, options12Hour); | ||
|
||
clock.innerHTML = ` ${time24Hour}<br>${time12Hour}`; | ||
|
||
clock12.innerHTML = `${time12Hour}`; | ||
clock24.innerHTML = `${time24Hour}`; | ||
|
||
|
||
day.innerHTML = ` ${date}`; | ||
|
||
} | ||
|
||
setInterval(updateClock, 1000); | ||
updateClock(); // Call it once immediately to display the time on page load | ||
updateClock(); |