console
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 html = document.querySelector('html');
toggle.addEventListener('click', () => {
if (html.classList.contains('dark')) {
toggle.textContent = 'Light Mode';
} else {
toggle.textContent = 'Dark Mode';
}
html.classList.toggle('dark');
})
function setTime() {
const date = new Date();
const second = date.getSeconds();
const minute = date.getMinutes();
const hours = date.getHours();
const day = date.getDate();
const [weekDay, month] = new Date().toDateString().split(' ');
const ampm = hours >= 12 ? 'PM' : 'AM';
const hoursForClock = hours > 12 ? hours % 12 : hours;
const minuteForClock = minute < 10 ? `0${minute}` : minute;
secondEl.style.transform = `translate(-50%, -100%) rotate(${second / 59 * 360}deg)`;
minuteEl.style.transform = `translate(-50%, -100%) rotate(${minute / 59 * 360}deg)`;
hourEl.style.transform = `translate(-50%, -100%) rotate(${hours / 23 * 360}deg)`;
timeEl.textContent = `${hoursForClock}:${minuteForClock} ${ampm}`;
dateEl.innerHTML = `${weekDay}, ${month} <span class='circle'>${day}</span>`
}
setTime();
setInterval(setTime, 1000);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>finished</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<button class="toggle">Dark Mode</button>
<div class="clock-container">
<div class="clock">
<div class="needle hour"></div>
<div class="needle minute"></div>
<div class="needle second"></div>
<div class="center-point"></div>
</div>
<div class="time"></div>
<div class="date"></div>
</div>
<script src="./script.js"></script>
</body>
</html>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
:root {
--primary-color: #000;
--second-color: #fff;
}
html {
transition: 0.3s ease-in;
}
html.dark {
--primary-color: #fff;
--second-color: #000;
background-color: #111;
color: var(--primary-color);
}
body {
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
overflow: hidden;
}
.toggle {
cursor: pointer;
background-color: var(--primary-color);
color: var(--second-color);
padding: 8px 12px;
border-radius: 4px;
position: absolute;
top: 100px;
}
.clock-container {
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
}
.clock {
position: relative;
width: 200px;
height: 200px;
}
.needle {
position: absolute;
top: 50%;
left: 50%;
height: 65px;
width: 3px;
background-color: var(--primary-color);
transform: translate(-50%, -100%) rotate(0deg);
transition: ease-in 0.3s;
transform-origin:bottom center;
}
.needle.minute {
height: 100px;
}
.needle.second {
height: 100px;
background-color: #e7434c;
}
.center-point {
border: 2px #e74c3c solid;
background-color: var(--primary-color);
width: 10px;
height: 10px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border-radius: 50%;
}
.time {
font-size: 60px;
}
.date {
color: #aaa;
font-size: 14px;
letter-spacing: 0.3px;
text-transform: uppercase;
}
.date .circle {
background-color: var(--primary-color);
color: var(--second-color);
border-radius: 50%;
height: 18px;
width: 18px;
display: inline-flex;
align-items: center;
justify-content: center;
line-height: 18px;
transition: all 0.5s ease-in;
font-size: 12px;
}