Skip to content

Instantly share code, notes, and snippets.

@quotepilgrim
Last active September 13, 2024 23:09
Show Gist options
  • Select an option

  • Save quotepilgrim/c05981125f6b442e126b9ce5d9074342 to your computer and use it in GitHub Desktop.

Select an option

Save quotepilgrim/c05981125f6b442e126b9ce5d9074342 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Current Time</title>
<link id="favicon" rel="icon" href="">
<style>
body {
background-color: #202020;
color:#ccc;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: serif;
}
#clock-container {
text-align: center;
}
#clock {
font-size: 18em;
}
#clock-small {
font-size: 0.6em;
}
#date {
color: #808080;
margin-top: 10px;
font-size: 4em;
}
</style>
</head>
<body>
<div id="clock-container">
<div id="clock">
<span id="clock-main">00:00</span>&VeryThinSpace;<span id="clock-small">00</span>
</div>
<div id="date">Date Loading...</div>
</div>
<script>
function updateFavicon(hours, minutes) {
console.log("genrated favicon");
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
canvas.width = 32;
canvas.height = 32;
ctx.clearRect(0, 0, canvas.width, canvas.height);
const fontSize = 20;
const verticalSpacing = 20;
const offset = 3;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#000";
ctx.font = `${fontSize}px Arial`;
ctx.fillText(hours, canvas.width / 2, canvas.height / 2 - verticalSpacing / 2 + offset);
ctx.fillStyle = "#666";
ctx.font = `${fontSize}px Arial`;
ctx.fillText(minutes, canvas.width / 2, canvas.height / 2 + verticalSpacing / 2);
const favicon = document.getElementById("favicon");
favicon.href = canvas.toDataURL("image/png");
}
function updateTime(favicon=false) {
const clockMain = document.getElementById("clock-main");
const clockSmall = document.getElementById("clock-small");
const date = document.getElementById("date");
const now = new Date();
const hours = String(now.getHours()).padStart(2, "0");
const minutes = String(now.getMinutes()).padStart(2, "0");
const seconds = String(now.getSeconds()).padStart(2, "0");
const options = { weekday: "long", month: "long", day: "numeric" };
const dateString = now.toLocaleDateString(undefined, options);
clockMain.textContent = `${hours}:${minutes}`;
clockSmall.textContent = seconds;
date.textContent = dateString;
document.title = `${hours}:${minutes}`;
if (seconds === "00" || favicon) {
updateFavicon(hours, minutes);
}
}
const now = new Date();
updateTime(true);
setInterval(updateTime, 1000);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment