-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d0a251
commit 155ad73
Showing
1 changed file
with
48 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,48 @@ | ||
const panels = document.querySelectorAll('.panel') | ||
const hourEl = document.querySelector('.hour') | ||
const minuteEl = document.querySelector('.minute') | ||
const secondEl = document.querySelector('.second') | ||
const timeEl = document.querySelector('.time') | ||
const dateEl = document.querySelector('.date') | ||
const toggle = document.querySelector('.toggle') | ||
|
||
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; | ||
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; | ||
|
||
toggle.addEventListener('click', (e) => { | ||
const html = document.querySelector('html') | ||
if (html.classList.contains('dark')) { | ||
html.classList.remove('dark') | ||
e.target.innerHTML = 'Dark mode' | ||
} else { | ||
html.classList.add('dark') | ||
e.target.innerHTML = 'Light mode' | ||
} | ||
}) | ||
|
||
function setTime() { | ||
const time = new Date(); | ||
const month = time.getMonth() | ||
const day = time.getDay() | ||
const date = time.getDate() | ||
const hours = time.getHours() | ||
const hoursForClock = hours >= 13 ? hours % 12 : hours; | ||
const minutes = time.getMinutes() | ||
const seconds = time.getSeconds() | ||
const ampm = hours >= 12 ? 'PM' : 'AM' | ||
|
||
hourEl.style.transform = `translate(-50%, -100%) rotate(${scale(hoursForClock, 0, 12, 0, 360)}deg)` | ||
minuteEl.style.transform = `translate(-50%, -100%) rotate(${scale(minutes, 0, 60, 0, 360)}deg)` | ||
secondEl.style.transform = `translate(-50%, -100%) rotate(${scale(seconds, 0, 60, 0, 360)}deg)` | ||
|
||
timeEl.innerHTML = `${hoursForClock}:${minutes < 10 ? `0${minutes}` : minutes} ${ampm}` | ||
dateEl.innerHTML = `${days[day]}, ${months[month]} <span class="circle">${date}</span>` | ||
} | ||
|
||
// StackOverflow https://stackoverflow.com/questions/10756313/javascript-jquery-map-a-range-of-numbers-to-another-range-of-numbers | ||
const scale = (num, in_min, in_max, out_min, out_max) => { | ||
return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; | ||
} | ||
|
||
setTime() | ||
|
||
setInterval(setTime, 1000) |