Binary clock
Create a binary clock that represents hours, minutes, and seconds using binary digits. This project not only gives you a visual representation of time in binary but also serves as a practical and aesthetic gadget.
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: 'Arial', sans-serif;
text-align: center;
margin: 50px;
}
#binary-clock {
font-size: 24px;
}
</style>
<script>
// function for converting the current time into binary
function updateBinaryClock() {
// create a date object
const now = new Date();
// get the current hour, minute, and second and convert it to a string using base 2.
// then, after converting it to a string. use pad start.
// padStart(targetLength, padString). targetLength is the length of the string. padString is the string that is padded with the original string
// pads the binary string on the left with zeros until it reaches a length of 4 characters.
// this is done to ensure that the binary representation always has 4/6 digits.
const hours = now.getHours().toString(2).padStart(4, '0');
const minutes = now.getMinutes().toString(2).padStart(6, '0');
const seconds = now.getSeconds().toString(2).padStart(6, '0');
// get ids of the html for hours, minutes, and seconds and then update it to the binary value
document.getElementById('hours').textContent = hours;
document.getElementById('minutes').textContent = minutes;
document.getElementById('seconds').textContent = seconds;
}
// run the function every second
setInterval(updateBinaryClock, 1000);
// run the binary clock function to initialize it
updateBinaryClock();
</script>
</head>
<body>
<img id="clock_image" src="../../../images/clock_image.jpg" alt="Clock Image">
<div id="binary-clock">
<div>Hours: <span id="hours"></span></div>
<div>Minutes: <span id="minutes"></span></div>
<div>Seconds: <span id="seconds"></span></div>
</div>
</body>
</html>